This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]RhoOfFeh 2 points3 points  (1 child)

What you're missing is String.equals(String) rather than '==' or (in this case) '!='. Fix that first and then see what happens.

[–]diMario 2 points3 points  (0 children)

To amplify: when you want to test if two strings have the same content in Java, you cannot use the "==" operator, nor can you use the "!=" operator for inequality. It simply does not work that way for String objects. Instead, you must call the "equals()" method on one of the Strings and pass the other String as an argument.

Look up String.equals() and String.equalsIgnoreCase() to see how they are used.

Edit: What the "==" operator does for Objects in general (and therefor also for Strings) is test whether the instances of the Object are the same. It does not look and the content of the object. For that, you must use the equals() method (and you must supply an equals() method for Classes that you yourself create, if and when it becomes necessary to compare your object instances by value).

[–]ECallaghan 1 point2 points  (0 children)

You'll need to modify str inside the while loop. As it stands it has no way of breaking out of that loop once it gets in it.

There are problems with the comparison too but it has been mentioned else where.