We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › comparing a user-entered char array to a string
Page Index Toggle Pages: 1
comparing a user-entered char array to a string? (Read 690 times)
comparing a user-entered char array to a string?
Oct 15th, 2007, 6:08pm
 
Ok, so I'm not entirely new to programming but I'm new to Java and my general knowledge is all over the place.

I'm trying to create an array from key presses entered by the user, and then I want to compare the array to a string taken from a text file.  So eventually I want two arrays.  I'm confused as to how to fill a character array one key press at a time.

I tried this:

char data[]= new char[10];
int index=0;

void keyPressed()
{

while(index<10)
{
 if(key=='a')
 {
   //add 'a' to the string
   data[index]='a';  
   index++;
 }
println(data + " ");
}
}


But it doesn't work-- it inputs an infinite number of key presses but doesn't go on to output statement. Adding a while loop isn't enough?
Re: comparing a user-entered char array to a strin
Reply #1 - Oct 15th, 2007, 10:02pm
 
I'm not entirely sure what's casing the problem you're seeing, but as an aside, you can read input into a String, rather than a char[], which will save you a lot of manual work in most cases as well as giving you easy access to comparing options.
Re: comparing a user-entered char array to a strin
Reply #2 - Oct 16th, 2007, 12:51am
 
do not use while(), since index++ only happens once, the while() loop will never finish, so the program can never continue (and keyPressed() will never be called again). in this case, use if().
Re: comparing a user-entered char array to a strin
Reply #3 - Oct 18th, 2007, 2:54am
 
Thank you for the advice!  I'm still a bit confused though.  I'm feeling really stupid since I saw something that worked a few weeks ago but now that I need it I can't find it.

JohnG--  Yes, actually I originally wanted to enter my input as a String.  I know how to do this in C++, but I can't find a Java/Processing equivalent to "cin."  There is for entering user input to a string in just one or two lines of code, isn't there?  The examples I am seeing all assign specific programmer-determined values to the string rather than taking user input.  

fry-- Thanks.  I had a feeling using while() was a bad idea, but I guess I don't understand how the key listener works.  I don't understand how to make it stop taking input after 10 key presses.  All I have managed to do is make the array go out of bounds by leaving the while() out.
Re: comparing a user-entered char array to a strin
Reply #4 - Oct 18th, 2007, 5:27am
 
Maybe something like:
Code:

String data[]= new String[10];
String test = "a";
int index=0;
void setup(){
}

void draw(){
}

void keyPressed(){
if(test.equals(String.valueOf(key))){
if(index < data.length){
data[index]="a";
index++;
println(data);
}else{
println("array full");
}
}
}
Re: comparing a user-entered char array to a strin
Reply #5 - Oct 20th, 2007, 12:05am
 
Jomo, thank you!  I still can't find the example I was using before, but your way works and solves my problem so I can go on to things that are more fun.  Kudos.
Page Index Toggle Pages: 1