I have this code and I want to catch the letter exception but it keeps having these errors:
Exception in thread \"main\" java.util.InputMismatchException
You can use a do-while loop instead to eliminate the first input.nextInt().
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);
Therefore all InputMismatchException can be handled in one place.