0

so ill just go directly to my problems. So my InputMismatchException won't work. Instead the one working is the exception i manually made from the other class called InvalidLetterException. If I enter a number the exception doing the catch is the InvalidLetterException instead of the InputMismatchException because im entering an integer, a different data type. I'm hinting theres a problem with my if else statement but i dont know what to do.

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    String questions[] = {"What is the color of the sky? ", "What is 1 + 1? ",
            "What is the capital of the Philippines? ", "Who is the current president of the Philippines? ",
            "What is the capital of Japan? ", "What is 2 + 3? ",
            "What is 9 + 1?", "What is the capital of the United States? ",
            "What is 10 + 10? ", "How many hand fingers do humans have? "};

    String choices[] = {"a","b","c"};


    int x = 0;
    int y = 0;



    try {

        while(x<=9) {

            System.out.println("No." + (x+1) + "." + questions[x]);
            String answer = scan.next();
            x++;

            if(answer.equals(choices[0])) {
                scan.nextLine();

            } else if (answer.equals(choices[1])) {
                scan.nextLine();

            } else if (answer.equals(choices[2])) {
                scan.nextLine();

            } else if (!answer.equals(choices)) {
                throw new InvalidLetterException(); 


            }

        } 

    } catch(InvalidLetterException e) {
        System.out.println(e.getMessage());
        System.out.println(); //Spacing
        System.out.println("You can try again.");
        System.out.println(); //Spacing
        do {
            System.out.println("No." + (y+1) + "." + questions[y]);
            scan.next();
            y++;
        }while(y<=9);

    } catch (InputMismatchException i) {
        System.out.println("Please don't enter numbers.");
    }



}
Pablo Job
  • 29
  • 4
  • `!answer.equals(choices)` will always evaluate to true. See the duplicate on how to verify if an array contains a certain value. – Robby Cornelissen Nov 10 '21 at 05:07
  • `String answer = scan.next();` anything can be taken as string. If you put number, it'll be taken as string. eg, entered :1, answer = "1" – sittsering Nov 10 '21 at 05:11

0 Answers0