11fish
11fish

Reputation: 19

Trying to find the indexOf a string from an ArrayList filled with strings from hashtable, only returning -1

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.

Upvotes: 0

Views: 124

Answers (3)

JB Nizet
JB Nizet

Reputation: 691715

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:

public void dumpString(String s) {
    System.out.println("String: " + s);
    System.out.println("Length: " + s.length());
    System.out.print("Chars: ");
    for (int i = 0; i < s.length(); i++) {
        System.out.print((int) s.charAt(i));
        System.out.print(' ');
    }
    System.out.println();
}

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500485

I know the string match in the fact that I test it visually by printing out the stringList.

That's probably the problem. There can be all kinds of subtle ways in which strings aren't actually equal, which don't show up by casual inspection. For example, if you have trailing Unicode NULL characters (U+0000) those probably wouldn't show up, and there are other non-printing characters too.

I suggest you iterate over stringList printing out the length of the string as well as the contents - then print the length of keyString too.

Also note that this code:

String keyString = "! Xxxx ##:##:## xx";
if(stringList.get(0).equals(keyString))
   System.out.println("true");
System.out.println("false");

... will always print false (assuming there's no exception). It may also print true, of course - but you're unconditionally printing false. It would be much simpler to just write:

String keyString = "! Xxxx ##:##:## xx";
System.out.println(stringList.get(0).equals(keyString));

Upvotes: 2

Elliott Hill
Elliott Hill

Reputation: 961

The string you are adding to your stung list is trimmed so any trailing or leading spaces will be removed. If the initial strings have spaces that could be your problem. Try removing the .trim() and see if that helps.

Upvotes: 1

Related Questions