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.
Page Index Toggle Pages: 1
boolean strings (Read 315 times)
boolean strings
Feb 11th, 2008, 11:07pm
 
I know this is probably a very simple problem but it is bugging the life out of me.

I am trying the following

Code:

if (stringA == "string") {
println("true")
}


However, it will not work. What am I doing wrong?

Thanks
Gary
Re: boolean strings
Reply #1 - Feb 11th, 2008, 11:25pm
 
String is an Object type, and therefore the == operator just compares whether an Object is the same Object as another.

Hence
Code:
String a="hello";
String b="hello";
if(a==b)
println("yes");
else
pritnln("no");

will print "no", but
Code:
String a="hello";
String b=a;
if(a==b)
println("yes");
else
pritnln("no");

will print "yes".

What you want is
Code:
if(stringA.equals("string"))
{
println(true);
}
Page Index Toggle Pages: 1