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 › split() return into conditional error...
Page Index Toggle Pages: 1
split() return into conditional error... (Read 818 times)
split() return into conditional error...
Sep 28th, 2008, 3:05am
 
Not sure if this is some kind of issue, or I'm just missing something important.  I could really use some help!

When I use split to parse a string, it successfully cuts up the string into elements of an array.  HOWEVER, when I then try to test an element in the resulting array in a conditional, it doesn't work.

For example, to use slightly modified code from the Reference section:

------------------------------------
String men = "Chernenko,Andropov,Brezhnev";
String[] list = split(men, ',');

println(list[0]);

if (list[0] == "Chernenko"){
 println("it worked!");
}
-------------------------------------

This code only outputs "Chernenko".  It does not print "it worked!", the conditional is not met.  Even though printing list[0] displays a value of "Chernenko", it does not recognize it as that in the conditional statement.


Did I explain that clearly?  Can any tell me why this isn't working... or what I am doing wrong?

Thanks!

Re: split() return into conditional error...
Reply #1 - Sep 28th, 2008, 9:44am
 
You can't use == with objects, this includes Strings. You have to use if(list[0].equals("Chernenko")){
Re: split() return into conditional error...
Reply #2 - Sep 28th, 2008, 10:33am
 
While we are in the doc bug section, I think I should take this opportunity to ask authors to fix the documentation of == (equality) reference page.
It is the third time in a short while that I see users trying == on strings, some of them pointing to this page.
Indeed, it states it applies to "int, float, char, byte, boolean, String".
I suggest you remove String: while it can work is some rare cases, it provides more confusion than help, I fear.

Perhaps even provide a hint to use .equals() to compare strings, I don't know if it is covered elsewhere in the documentation.
Re: split() return into conditional error...
Reply #3 - Sep 28th, 2008, 5:30pm
 
Oh, odd.  Thanks for the correction!
Re: split() return into conditional error...
Reply #4 - Sep 28th, 2008, 11:27pm
 
This is already covered in the String reference:
http://processing.org/reference/String.html

And the troubleshooting page (Help > Troubleshooting):
http://processing.org/reference/troubleshooting/index.html#strings

I've now added it to the == reference as well for release 0149, but I doubt it'll make any difference.

For reference requests, please file bugs so that we can fix them. You can't count on Casey and I reading every single post on the board.
Re: split() return into conditional error...
Reply #5 - Sep 29th, 2008, 10:11pm
 
You are right, I should have made a more formal request, sorry for that.
Re: split() return into conditional error...
Reply #6 - Sep 30th, 2008, 1:49am
 
PhiLho  wrote on Sep 29th, 2008, 10:11pm:
You are right, I should have made a more formal request, sorry for that.

Sorry, didn't mean that to sound like a scold--you help us with enough things (filing bugs, answering questions) that you're the least of our worries. Wink
Re: split() return into conditional error...
Reply #7 - Oct 2nd, 2008, 1:17pm
 
No problem, I just took it as a rightful, gentle reminder! :-D
Page Index Toggle Pages: 1