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
listFiles() bug? (Read 467 times)
listFiles() bug?
Feb 16th, 2010, 9:38am
 
Hi,

I am trying to do string comparisons on lists of files. I found this to be a problem so I reduced it to this. Fill in the name of the file you are looking for in y and then pathtoafile is the system path to the file. When you first run the program copy and paste the string printed in the output to string y and it won't come back as true.

When I do simple string comparison I get a true but the string that is returned and printed from File.listFiles() doesn't seem to be equivalent. Any help is appreciated.


String y = "somefile.jpg ";
String pathtoafile = "/FILL/IN/THE/PATH/HERE";

File pathtoimages = new File(pathtoafile);
File[] files = pathtoimages.listFiles();

println(files[1].getName());

if (y == files[1].getName())
println("true");
Re: listFiles() bug?
Reply #1 - Feb 16th, 2010, 9:50am
 
You should read attentively the String reference before suspecting a bug in Java... Smiley

Hint: don't use == to compare strings.
Re: listFiles() bug?
Reply #2 - Feb 17th, 2010, 1:33pm
 
Problem solved! Turned out it was user error. Thanks a lot!
Page Index Toggle Pages: 1