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

25th Aug 2018, 10:50 AM
M.A
M.A - avatar
3 Respostas
+ 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"))
25th Aug 2018, 10:58 AM
Hatsy Rei
Hatsy Rei - avatar
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.
25th Aug 2018, 11:02 AM
TurtleShell
TurtleShell - avatar
25th Aug 2018, 11:54 AM
M.A
M.A - avatar