+ 1

Java - how to empty scanner?

Hello everyone. I am making a bit more complex program in java and need help. I ask user to type a command and then I do a certain action depending on that command. Currenetly I test it only on one possible command - pay. Syntax of the command must be following: "pay <name (string)> <amount (int)> ". It works quite nicely: if a user types two strings instead of a string and an int, it throws exception, which is catched and it prints correct syntax to the user. But there´s one problem: If a user types e.g. "pay PlayerA seven", it prints correct syntax, but next time it takes "seven" as the first argument (command name). So when the user types "pay PlayerA 75" as the second try, it takes "seven pay PlayerA" instead. How can I make the scanner to forget everything that was typed before?

13th Apr 2018, 2:44 PM
Jan Štěch
Jan Štěch - avatar
9 Answers
+ 7
Jan Štěch You can do the following three tweaks, one of them would be useful for sure. Tweak 1: cmd = input.next(); arg1 = input.next(); arg2 = input.nextInt(); input.next(); input.nextLine(); input.nextLine(); System.out.println(cmd); ... Tweak 2: try { //... } catch (InputMismatchException ex) { input.next(); // Read and discard whatever string the user has entered } Tweak 3: if(!input.hasNext() || !input.hasNextInt()) { input.next(); continue; } int cmd = input.next(); ... I'm sure you'd work it out. Though if you're still unable, you should move on from Scanner to other input Classes and methods. Check, https://www.geeksforgeeks.org/ways-to-read-input-from-console-in-java/ Use BufferedReader class or Console class to do the same! :)
14th Apr 2018, 5:19 AM
Dev
Dev - avatar
+ 5
Actually, you cannot empty or clean the Scanner buffer explicitly. So nextLine() is helpful here. How about taking the Scanner stream to the next line as soon as the arg2 integer input is taken? Adding input.nextLine(); immediately after arg2 = input.nextInt(); might help! Find out more info about nextLine() here: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()
13th Apr 2018, 3:24 PM
Dev
Dev - avatar
+ 4
Jan Štěch Hey, I've found something that could better resolve your issue. Go through the very first answer here and try implementing it in your code. https://stackoverflow.com/questions/15973040/scanner-reset-doesnt-work See if it works... :)
13th Apr 2018, 5:14 PM
Dev
Dev - avatar
+ 4
Jan Štěch hasNext() returns a Boolean value (either true or false). You might not want to print it. Check: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNext() Just use it to check the condition in the if statement. Don't print it. Use input.next() instead, only! It is midnight here, would look deep into it tomorrow morning. Going to sleep soon zZZ
13th Apr 2018, 6:35 PM
Dev
Dev - avatar
+ 1
I did it just as you wrote, but it still works the same way. I saw a method called input.reset and input.remove, but I can´t work with it.
13th Apr 2018, 4:30 PM
Jan Štěch
Jan Štěch - avatar
+ 1
Thank you very much. I think I got what I need to do.
13th Apr 2018, 5:49 PM
Jan Štěch
Jan Štěch - avatar
+ 1
Ok, so I did what was written there, but it still doesn´t work. I put this: while(input.hasNext()==true) {System.out.println(input.hasNext());input.next()} into catch block. I guess it should discard everything in input and move to the end of typed text. But instead of it it just askes me for input again and again. The input.hasNext() condition is alway met. Why? Output is like this (- signs my input): -pay Player seven Invalid arguments. Usage: pay <player> <amount> true -pay Player 7 true true true -some input true true -someInput true
13th Apr 2018, 6:20 PM
Jan Štěch
Jan Štěch - avatar
+ 1
I know it just tests if there is a following word. I print it only because I want to see if it returns false sometimes. As I see, it just asks me for more input, when it runs out of words and should return false and exit the cycle. And it's the whole problem. I just need it to exit the cycle instead of asking for more words. If I understood it correctly, .next() skips and returns next word in case there is some, or pauses the program and waits for new words from a user in case there isn't any next word. Have nice dreams and thank you for today. 😃😴.
13th Apr 2018, 8:24 PM
Jan Štěch
Jan Štěch - avatar
0
Here is my program: Scanner input=new Scanner(System.in); String cmd=""; String arg1=""; int arg2=0; while(true) { try { cmd = input.next(); arg1 = input.next(); arg2 = input.nextInt(); System.out.println(cmd);//Some action to perform on correct syntax System.out.println(arg1); System.out.println(arg2); } catch(InputMismatchException e) { System.out.println("Invalid arguments. Usage: "+cmd+" <player> <amount>"); continue; } break; } input.close();
13th Apr 2018, 2:48 PM
Jan Štěch
Jan Štěch - avatar