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 › adding a char to end of string problem
Page Index Toggle Pages: 1
adding a char to end of string problem (Read 229 times)
adding a char to end of string problem
Jul 30th, 2008, 5:17am
 
/*
run this code below to illustrate my problem
how do I properly add a character to the end of a string so the if statements return true?
*/

void checkMatch(String temp)
{
 print(temp);
 println(" == AA");
 
 print("is this a match? ");
 
 if(temp == "AA")
 {
   println("yes");
 }
 else
 {
   println("no");
 }
 
 println("why?\n\r");
}

void setup()
{
 String temp = "";
 temp += 'A';
 temp += "A";
 
 checkMatch(temp);
 
 temp = "";
 temp += "A";
 temp += "A";
 
 checkMatch(temp);
 
 temp = "AA";
 
 checkMatch(temp);
}
Re: adding a char to end of string problem
Reply #1 - Jul 30th, 2008, 5:51am
 
A string is a class, so normal if checks won't work. When you check one string against another like temp == "AA" you are actually checking the memory location of the string. By using the String.equals("blah") function it will check the letters to see if they match.

So try this instead

Quote:

void checkMatch(String temp)
{
 print(temp);
 println(" == AA");
 
 print("is this a match? ");
 
 if(temp.equals("AA") == true) // I changed this line
 {
   println("yes");
 }
 else
 {
   println("no");
 }
 
 println("why?\n\r");
}

void setup()
{
 String temp = "";
 temp += 'A';
 temp += "A";
 
 checkMatch(temp);
 
 temp = "";
 temp += "A";
 temp += "A";
 
 checkMatch(temp);
 
 temp = "AA";
 
 checkMatch(temp);
}


Page Index Toggle Pages: 1