I have two arraylists, one contains Strings and the other contains Classes that have a String data member. The goal is to recurse through the objects in the second arraylist, and find which object’s string data member equals a string value in the first arraylist.
Now I understand that you cannot use the == operator to compare strings, so I initially used the equals method, and now I’ve tried the contentEquals method, however regardless of the method I choose the strings are always classified as equal – even when they are not. So whenever I run the code below, the “if” statement always returns true the first time it is called, regardless of whether the strings are actually equal.
Iterator stringListIter = stringList.iterator();
Iterator objectListIter = objectList.iterator();
while (stringListIter.hasNext())
{
String currentString = (String) stringListIter.next();
while (objectListIter.hasNext())
{
MyObject currentObject = (MyObject) objectListIter.next();
String objectString = currentObject.getString();
if (objectString.contentEquals(currentString));
{
//Do something here.....
break;
}
}
}
Any help would be greatly appreciated. I get the feeling its probably something that is fairly simple, but I just can’t see it.
The problem is you have an extraneous semicolon:
This statement essentially does nothing so the following block is always executed.