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 & HelpPrograms › Trouble comparing strings within an array
Page Index Toggle Pages: 1
Trouble comparing strings within an array (Read 572 times)
Trouble comparing strings within an array
Sep 13th, 2008, 2:22am
 
I expect this has a very simple solution that I am simply overlooking.  I'm hoping that you'll spot it.

I'm working on a data visualization of my iTunes library.  It reads the library into memory and creates an array of "Track" object (called "tracks") for each item in the library.  Each Track object stores information like the track's title, length, genre, and so on.

I'm interested in the genre right now.  So I have thousands of Track objects, and I want to figure out how many unique genres there are (e.g. "Rock," "Electronic" and so on).  The genres are stored as Strings within each object.

I thought I would try this:  Create a String array and fill it with the genres of all the tracks.  Then alphabetize the array, loop through it and do a != comparison to skip over the duplicates.  Read out the unique genres into a separate array.  Then the second array.length would be the number of unique genres, and the array itself would contain the String text of each genre.

Sounds okay, right?  Well, the code below doesn't work.  It seems to come up with the correct count in the end, so the uniqueGenres array length is 77, for example, but the contents of each element in the array is blank, or " ".  I would expect the elements to be things like "Pop" and "Jazz" (although likely not "Gangsta Rap").

If anyone has some ideas, please let me know.  Thanks!

Code:

//Create a new string array to hold the names of all genres (including dupes)
String allGenres[] = new String[tracks.length];
for (int i = 0; i < tracks.length; i++) {
allGenres[i] = tracks[i].genre;
}
//Alphabetize the whole list
allGenres = sort(allGenres);

//Establish unique (non-dupe) genre array, and prepopulate it with the first genre
String uniqueGenres[] = new String[1];
uniqueGenres[0] = tracks[0].genre;

for (int i = 1; i < allGenres.length; i++) {
if (allGenres[i] != allGenres[i-1]) {
}
else {
println(allGenres[i]);
String tempNewGenre = allGenres[i];
uniqueGenres = append(uniqueGenres, tempNewGenre);
}
}

for (int i = 0; i < uniqueGenres.length; i++) {
println(uniqueGenres[i]);
}
println("uniqueGenres.length is " + uniqueGenres.length);

Re: Trouble comparing strings within an array
Reply #1 - Sep 13th, 2008, 5:37am
 
See Help -> Troubleshooting under "why don't these strings equal".

http://processing.org/reference/troubleshooting/index.html#strings
Re: Trouble comparing strings within an array
Reply #2 - Sep 13th, 2008, 5:55am
 
Yes!  Thank you, thank you, thank you.  I love when it's something simple like that.
Re: Trouble comparing strings within an array
Reply #3 - Sep 13th, 2008, 6:01am
 
Using != on Strings will not work, because you are comparing objects (actually pointers). It would be a terrific coincidence if the objects were equal. Possible, because Strings in Java are immutable and thus often shared.

Anyway, use the equal() method. For instance:

String a = "....";
String b = xxx.getString();
if (a.equals(b)) {
  do something;
}

Hope this helps!
Re: Trouble comparing strings within an array
Reply #4 - Sep 13th, 2008, 6:05am
 
Thanks, abonnema.  Yes, that works perfectly.

Is there also a .notEquals method, or some other equivalent of "!="?

Currently, I'm using:

if (a.equals(b)) {
    //do nothing, since a == b
} else {
    //do something, since a != b
}
Re: Trouble comparing strings within an array
Reply #5 - Sep 13th, 2008, 6:56am
 
Something like if (! a.equals(b))
Re: Trouble comparing strings within an array
Reply #6 - Sep 13th, 2008, 10:36am
 
fry wrote on Sep 13th, 2008, 5:37am:
See Help -> Troubleshooting under "why don't these strings equal".

http://processing.org/reference/troubleshooting/index.html#strings

Aha! So the issue was addressed somewhere. I think this info should be in == (equality) reference page.

In help a beginner draw data from tsv I pointed out a little ambiguity in this page.
Page Index Toggle Pages: 1