0
How do I interact with Scanners properly?
I didnt know how to approach scanners, so i tried to make a project, and as expected, a fail. you can check the code here https://code.sololearn.com/c3qMoautx31B/?ref=app If i type "start", the thing just stops working, tried it with different ides. if i enter start it just stops the program
3 Answers
+ 5
Two things.
.next() for scanner gets the next character only, so use .nextLine() instead, to achieve your goal.
To compare the value of strings, use .equals() (compares value) instead of == (compares references).
if (MyVar.nextLine().equals("start"))
0
The problem is with the string comparison, when comparing strings always use .equals() method instead of == e.g.
str.equals("start")
Another thing you probably want to change is putting user input in a variable and use .nextLine() e.g.
String userInput = myVar.nextLine();
This is because every time you call .nextLine() Java will ask the user for another input which I'm guessing is not what you want.
0
Thanks TurtleShell and Hatsy Rei