+ 1
In java exception handling how can i loop it back
In java exception handling how can i loop it back so that i can take correct input again if like for example input mismatch exception were to happen
1 Antwort
+ 3
Use do-while as below
do {
try {
System.out.print("Enter the number of students: ");
students = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("Invalid number of students. ");
}
input.nextLine(); // clears the buffer
} while (students <= 0);