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 › Simple string comparison problem
Page Index Toggle Pages: 1
Simple string comparison problem (Read 772 times)
Simple string comparison problem
Dec 21st, 2009, 2:08pm
 
Hey,

Sorry if this has been asked before, I searched the forums, and couldn't find anyone with the same problem.

I am doing a simple comparison between two strings. If they do not equal each other, I want it to fire a function I wrote. It should be simple. Yet, my program always thinks the strings are not equal, even when they are.

I did a println of the two strings right before the comparison, and they both equal "p64". I thought maybe I perhaps had an accidental space being added to the end of one of the strings, so I checked their lengths, and they are both 3 characters long. They are absolutely identical in everyway that I can tell, yet processing never recognizes them as being equal, and always fires on the does not equal comparison.

Is this a processing bug, or I just completely missing something?

Thanks in advance for nay help.

-Thomas
Re: Simple string comparison problem
Reply #1 - Dec 21st, 2009, 2:14pm
 
OK.. minutes after I wrote this, I found a solution. If I use:

if (c.equals(prevString) == false) {}

as opposed to

if (c != prevString) {}

it works. I'm still baffled by why one would work over the other..

-Thomas
Re: Simple string comparison problem
Reply #2 - Dec 21st, 2009, 2:28pm
 
What version of Processing are you using?  It might also help to include the code where you set the String variables to see what's happening at that end.

Curiously my test seems at odds with the documentation:

Code:
String foo = "p64";
String bar = "p64";

void setup() {
 size(100,100);
 if(foo == bar) {
   println("equal");
 }
 
 if(foo.equals(bar)){
   println("equal too");
 }
 
}


From equals():

"This method is necessary because it's not possible to compare strings using the equality operator (==). "

That would suggest that there's some underlying reason why a standard comparison might not work; though when I run the above both conditions return true  Shocked
Re: Simple string comparison problem
Reply #3 - Dec 21st, 2009, 2:29pm
 
this is standard Java stuff and it's because String is a class, not a base type. (a class is a box, you wanted to compare the *contents* of two boxes, not the boxes themselves)

it's also mentioned in the processing reference for String:
http://processing.org/reference/String.html

grrrrrr.... 8)
Re: Simple string comparison problem
Reply #4 - Dec 21st, 2009, 2:56pm
 
blindfish wrote on Dec 21st, 2009, 2:28pm:
That would suggest that there's some underlying reason why a standard comparison might not work; though when I run the above both conditions return true  Shocked


The compiler probably did some optimizations and detected that 2 different variables were in fact the same string and he probably used the same bit of memory for both variables.

This bit of code only prints the second equal:

Code:
String foo = "p64";
String bar = "p6";

void setup() {
 size(100,100);
 
 bar += "4";
 if(foo == bar) {
   println("equal");
 }
 
 if(foo.equals(bar)){
   println("equal too");
 }
 
}
Re: Simple string comparison problem
Reply #5 - Dec 21st, 2009, 3:02pm
 
koogy wrote on Dec 21st, 2009, 2:29pm:
this is standard Java stuff and it's because String is a class, not a base type. (a class is a box, you wanted to compare the *contents* of two boxes, not the boxes themselves)

it's also mentioned in the processing reference for String:
http://processing.org/reference/String.html

grrrrrr.... 8)


Doh!

Embarrassed

(I'll use the excuse that I would have expected the OP to look there first Wink )
Page Index Toggle Pages: 1