I have been looking for solution and tried it on my code, but still no luck found. What I want to do is to throw an exception if the user inputs nothing and just presses enter and a catch block will be executed once it throws the exception. But in my case, it only executes the second catch-block I have which is the InvalidAnswerException
instead of BlankException
Code:
import java.util.Scanner;
public class QnA {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
String[] questions = { "Question-1: Who is the national hero of the Philippines?", "Question-2: What is the national Anthem of the Philippines ?",
"Question-3: What is the national sport of the philippines ?"};
String choices[][] = { { "A)Jose Rizal", "B)Andres Bonifacio", "C)Lapu-Lapu" },
{ "A)Paro-Paro G", "B)Bahay kubo", "C)Lupang Hinirang" }, { "A)Basketball", "B)Arnis", "C)Badminton" }, };
char answers[] = {'A', 'B', 'C'};
int score = 0;
for (int i = 0; i < 3; i ) {
while (true) {
System.out.println(questions[i]);
for (int j = 0; j < 3; j )
System.out.println(choices[i][j]);
System.out.print("Your Answer : ");
String ans = sc.nextLine();
try {
if (ans.contains("A") || ans.contains("a") || ans.contains("B") || ans.contains("b") || ans.contains("C") || ans.contains("c")) {
score ;
break;
}
if (ans.matches("[0-9]") || ans.matches("[!@#$%&*()_ =|<>?{}\\\\[\\\\]~-]")) {
throw new InvalidInputException("");
}
if (!ans.equalsIgnoreCase("A") || !ans.equalsIgnoreCase("B") || !ans.equalsIgnoreCase("C")) {
throw new InvalidAnswerException("");
}
if (ans.length() == 0) {
throw new BlankException("");
}
} catch (InvalidInputException e) {
System.out.println("InvalidInputException,Try again..");
continue;
} catch (InvalidAnswerException e) {
System.out.println("InvalidAnswerException,Try again..");
continue;
} catch(BlankException e) {
System.out.println("BlankException,Try again..");
continue;
}
}
}
System.out.println("Your score out of " questions.length " is : " score);
sc.close();
}
}
class InvalidInputException extends Exception {
public InvalidInputException(String message) {
super(message);
}
}
class InvalidAnswerException extends Exception {
public InvalidAnswerException(String message) {
super(message);
}
}
class BlankException extends Exception {
public BlankException(String message) {
super(message);
}
}
CodePudding user response:
As @Ole V.V. pointed out, your last if (ans.legnth() == 0)
condition is not applied because it is not even reached by your program. When user inputs nothing ""
your ans
variable becomes ans=""
and then it applies to the third condition if (!ans.equalsIgnoreCase("A") || !ans.equalsIgnoreCase("B") || !ans.equalsIgnoreCase("C"))
. The simplest solution of this is to put your last if
before any other. So, your code may be changed like that:
while (true) {
System.out.println(questions[i]);
for (int j = 0; j < 3; j )
System.out.println(choices[i][j]);
System.out.print("Your Answer : ");
String ans = sc.nextLine();
try {
if (ans.contains("A") || ans.contains("a") || ans.contains("B") || ans.contains("b") || ans.contains("C") || ans.contains("c")) {
score ;
break;
}
if (ans.length() == 0) {
throw new BlankException("");
}
if (ans.matches("[0-9]") || ans.matches("[!@#$%&*()_ =|<>?{}\\\\[\\\\]~-]")) {
throw new InvalidInputException("");
}
if (!ans.equalsIgnoreCase("A") || !ans.equalsIgnoreCase("B") || !ans.equalsIgnoreCase("C")) {
throw new InvalidAnswerException("");
}
} catch (InvalidInputException e) {
System.out.println("InvalidInputException,Try again..");
continue;
} catch (InvalidAnswerException e) {
System.out.println("InvalidAnswerException,Try again..");
continue;
} catch(BlankException e) {
System.out.println("BlankException,Try again..");
continue;
}
}