Saya memiliki loop do-while
yang tidak berfungsi. Ketika saya memasukkan String dan bukannya int, seharusnya tertulis "bla" dan meminta lagi untuk memasukkan nomor, namun malah mengirim teks pesan berulang-ulang. Apa yang salah dalam kode ini?
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.
. Jadi, Anda harus melewati token setelah Anda mendapatkanInputMismatchException
. - person Andreas Fester   schedule 01.07.2013