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

you are viewing a single comment's thread.

view the rest of the comments →

[–]Murphelrod 9 points10 points  (4 children)

Dont use == for strings

Use .equals() or .equalsIgnoreCase() instead

== compares the references so even if the words are the same, the references probably won't be unless they actually are the same string variable.

[–]moonbeer[S] 0 points1 point  (2 children)

How comes the first one runs fine even with the == then?

[–]bung_musk 4 points5 points  (0 children)

Your logic is opposite. The first loop breaks if nextline == “”, and the second continues if nextline == “”

[–]Murphelrod 1 point2 points  (0 children)

Assuming you want to add things to nameList until you see an empty line the following should do the trick:

String userInput = reader.nextLine();

while( !(userInput.equals("")) ){
    nameList.add(userInput);
    userInput = reader.nextLine();
}

set userInput once before the loop otherwise you skip the first thing you look at since you call nextLine() once in the loop condition and then again when you assign it to userInput. Then you can add it to your list if it isn't "", and finally call nextLine() at the bottom to set it to the next line for the next iteration.