static player scan_info_play() { Scanner in = new Scanner(System.in);//1 Variant player player__; String name; int num; while (true) { try { //Scanner in = new Scanner(System.in);//2 Variant System.out.println("Введите данные игрока :\n"); System.out.println("Имя :"); name = in.next(); System.out.println("Номер :"); num = in.nextInt(); break; } catch (java.util.InputMismatchException e) { System.out.println("UNCORRECT DATA !"); continue; } } player__ = new player(name, num, false); return player__; 

1 variant 2 variant

What is the question itself .. When I use option 1, the program does not work correctly. I can’t understand why after the error has been triggered and processed, I’m not returning to the beginning of the loop using the continue operator, but it turns out in the middle. Why does it print all the text to me at first, but does not ask me to fill in the name field again (somehow skips it) asks for the number, not the name as in option 2. As I understand it, for some reason, it remembers my previous input and does not overwrite it. How does the place of the scanner announcement affect here, but the course of the cycle actions?

    2 answers 2

    In the first case, we use the same Scanner, in the second case, after each error, a new one is created. Inside, the Scanner is designed in such a way that, after reading it with the help of the next or nextSomething method, and if it fails, it will retain what it was not possible to assign to the variable after the reading — in itself. The next time you call the next or nextSomething method on the same Scanner, the old information will try to be assigned to the next variable, which we are trying to return the result of the next or nextSomething method, and if the data type fits, everything will be assigned.

    For example, if we change the order of the input name and number, next and nextInt, make a mistake when entering the number, an infinite loop will occur, which will throw an error back to the beginning of the cycle, and try to assign incorrectly entered data that is not an int - variable int, or t .d:

     import java.util.Scanner; public class A { public static void main(String[] args) { scan_info_play(); } static player scan_info_play() { Scanner in = new Scanner(System.in);//1 Variant player player__; String name; int num; while (true) { try { //Scanner in = new Scanner(System.in);//2 Variant System.out.print("Data:\n"); System.out.print("No:"); num = in.nextInt(); System.out.print("Na:"); name = in.next(); break; } catch (java.util.InputMismatchException e) { System.out.println("UNCORRECT DATA !"); continue; } } player__ = new player(num, name, false); return player__; } } class player { // Результат "[Бесконечный цикл]" player(int num, String name, boolean b){ System.out.println(num + " " + name + " " + b); } } 

    In the second, swap the next and nextInt, do as in your case, make a mistake when entering int, incorrectly entered data that should have been int, will be easily assigned to the String type in the next iteration, after the next () method is executed;

     import java.util.Scanner; public class A { public static void main(String[] args) { scan_info_play(); } static player scan_info_play() { Scanner in = new Scanner(System.in);//1 Variant player player__; String name; int num; while (true) { try { //Scanner in = new Scanner(System.in);//2 Variant System.out.print("Data:\n"); System.out.print("Na:"); name = in.next(); System.out.print("No:"); num = in.nextInt(); break; } catch (java.util.InputMismatchException e) { System.out.println("UNCORRECT DATA !"); continue; } } player__ = new player(name, num, false); return player__; } } class player { player(String name, int num, boolean b){ System.out.println(name + " " + num + " " + b); // Результат ["неправильно введённый в первый раз int", "int на второй итерации", "false"] } } 

      In short: There is no buffer in the event that an exception is generated in the Scaner mechanism.

      A little more: The case in the device of the mechanism of the class Scaner . If you look at the flow of the process of getting a string from an impuls stream and also casting it to the Integer type, you will see that the validation of the string and the subsequent generation of the exception occurs without clearing the buffer of the scanner itself.

      And by going in the next iteration loop accordingly, you automatically apply the string you received to enter the number to the name of your player.

      For your case in the Scaner class, the reset() method is suitable, although judging by the method’s sorsam, this will at least turn back on, apart from cache collection, and reset locale (and this already involves other interesting questions)

      Do not forget about debag)