I am trying to get the indexOf of a specific string from an ArrayList but the string is not matching when doing the check. I am filling the StringList from hashtable with the code below.
stringList = new ArrayList<String>
Set st = hashtable.keySet();
Iterator itr = st.iterator();
while(itr.hasNext())
{
stringList.add(((String)itr.next()).trim());
}
The string I am trying to get from the stringList is input by the user, the string is generally in the form of “! Xxxx ##:##:## xx” if that makes a difference. I know the string match in the fact that I test it visually by printing out the stringList. So I figure it is something similar to the == and .equals().
I tested also by filling the stringList with a specific String from the hashtable and using the code below and it always returns false.
String keyString = "! Xxxx ##:##:## xx";
if(stringList.get(0).equals(keyString))
System.out.println("true");
System.out.println("false");
So I am trying to figure out how to get the code below to work
stringList.indexOf(keyString)
because it only returns -1 which it should, as it is not seeing the strings are equal. What am I doing wrong that is preventing the output I am looking for, which would be the correct index that I think it should be returning since I am typing exactly what is being print out by stringList. Like I said before, I thought it was an issue with .equals() but from what I can tell indexOf uses .equals() when looking for the index as well as seeing the results from me testing .equals() myself.
As Jon says, there’s probably a difference in the actual characters of the two strings, that doesn’t appear visually.
Pass the two Strings that are supposed to be equal to the following method to know where the problem is: