Reputation: 43
if I was not really stuck I wouldnt post here. I am trying to compare 2 Strings with equals(), sadly it never goes through the if function. here is the code :
public ListElement searchElement(String o) {
ListElement le = first;
while (le != null) {
System.out.println("NAME : " + le.getName() + " : " + o + " " + le.getName().equals(o));
if (le.getName().equals(o))
{
return le;
}
le = le.next;
}
return null;
}
and here is the output :
NAME : aa : aa false
NAME : cc : aa false
NAME : bb : aa false
NAME : aa : bb false
NAME : cc : bb false
NAME : bb : bb false
NAME : aa : cc false
NAME : cc : cc false
NAME : bb : cc false
NAME : aa : aa false
NAME : cc : aa false
NAME : bb : aa false
NAME : aa : bb false
NAME : cc : bb false
NAME : bb : bb false
NAME : aa : cc false
NAME : cc : cc false
Every time I compare 2 equal String, the programm does not see it. Any hints please ?
Upvotes: 0
Views: 287
Reputation: 2907
Each second string has two extra spaces at the end; this can be resolved by changing le.getName().equals(o)
to le.getName().equals(o.trim())
. The trim()
method in String
gets rid of any extra spaces at the beginning or end of the given string.
Upvotes: 1