I am just beginning to learn Java I am working on a quiz and I want to use Exception handling for some errors that might happen. 1. The user should only answer A,B,C if they answer D or any other letters there must be an exception then they can try again 2. The user should not have blank answers, if they leave a blank on a question there must be an exception then they can try again
And is it recommended to use switch case for this? (this is what I've done)
try {
String answer1 = scan.next();
switch(answer1.toUpperCase()) {
case "A":
System.out.println("Wrong, correct answer is B");
break;
case "B":
score++;
System.out.println("Correct!");
break;
case "C":
System.out.println("Wrong, correct answer is B");
break;
default:
throw new InputMismatchException();
}
} catch (InputMismatchException ex) {
System.out.println("INVALID! Must be letters only, Try again");
}
how will I do the other exceptions ? When I try to run it when the user input d it will also print Invalid must be letters only .. Thank you :)