Your scanner.nextLine() should be outside your while. And generally do-while is used better in combination with switch-case.. So, you can better go with it..
A general use of do-while with switch-case, when you are reading from user is like this: -
boolean flag = false;
do {
String option = scanner.nextLine();
switch (option) {
case "1": break;
case "2": break;
default : flag = true;
break;
}
} while(! flag);
And one important thing to know is: - Strings are Immutable in Java..
Generally for String comparisons in Java, you should use equals() function if you want to compare their values.. And also, I suggest you to override this equals() and hashcode() methods in your class (In future if you make one), to compare two instances of your class..
Actually, there is a big difference between == and equals().
It actually depends on how you are creating your string..
For E.g., suppose this is your code: -
String a = "5";
String b = new String("5");
String c = "5"; or String c = a;
System.out.println("a == b?" + (a == b)); // Will Print false
System.out.println("a.equals(b)?" + a.equals(b)); // Will Print true..
System.out.println("a == c?" + (a == c)); // Will Print true
System.out.println("a.equals(c)?" + a.equals(c)); // Will Print true..
In the above code, your String references 'a' and 'c' are pointing to the same object represented by "5". Whereas the reference "b" is pointing to a different object "5", because using "new" operator to create a String always creates a new Object..
Now, == operator checks whether the two String reference being compared points to the same object or different, whereas equals() function compares the actual value contained by the two string objects..
So, the output in the program will be clear now..
I just explained this because, in your while, you will have to use equals() function to check whether your "option" reference has the same value as "5".
Now, I think you can change your code a little bit, and preferably use do-while to get required output..