У меня есть неработающий цикл do-while
. Когда я ввожу String вместо int, он должен сказать "bla" и снова попросить вставить число, но вместо этого он снова и снова отправляет текст сообщения. Что не так в этом коде?
boolean i = true;
do {
i = false;
try {
System.out.println("insert number");
int k = sc.nextInt();
}
catch(InputMismatchException e) {
System.out.println("test");
i = true;
}
} while ( i== true);
When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.
. Итак, вам нужно пропустить токен, как только вы получитеInputMismatchException
. - person Andreas Fester   schedule 01.07.2013