L M
L M

Reputation: 65

How to quit program by entering specific letter while inside a Try-Catch While Loop?

Basically I want to exit the program when the user enters the letter "q" instead of an integer.

Been trying for a couple of hours, tried to solve it by adding

if(quit.equalsIgnoreCase("q")){
System.exit(0)
}

In the try statement. Tried removing the Scanner from the try statement and adding it before the while loop, then making a new variable like so:

String quit = "";
while (quit != "q"){
      //code
}

then adding a way to quit later in the code again, but that didn't work.

 while (true) {

                try {
                    int randomNumberOne = ThreadLocalRandom.current().nextInt(10, 21); //generates a number between 10 and 20
                    int randomNumberTwo = ThreadLocalRandom.current().nextInt(10, 21); //generates a number between 10 and 20
                    System.out.println("Type and enter \"q\" to quit at any time \n \n");

                    System.out.println(randomNumberOne + " % " + randomNumberTwo + " = ?"); //prints the question

                    Scanner userInput = new Scanner(System.in);
                    int remainderInput = userInput.nextInt();

                    if (remainderInput == randomNumberOne % randomNumberTwo) { //if they get the question right
                        userScore += 20; //adds 20 points
                        performance += 1; //adds 1 to the correct question counter
                        performancetotal += 1; //adds 1 to the total question counter
                        System.out.println("Correct answer, Current Score: " + userScore + ", performance: " + performance + "/" + performancetotal + "\n");
                        continue;
                    }

                    else { //if they get the question wrong
                        userScore += 0; //adds no points
                        performance += 0; //adds nothing to correct question counter
                        performancetotal += 1;
                        System.out.println("Incorrect answer, Current Score: " + userScore + ", performance: " + performance + "/" + performancetotal + "\n");
                        continue;
                    }

                 }

                catch (InputMismatchException e) {
                    System.out.println("Invalid input\n");

                }

         }
    }

This is my current code except for some variable at the top which shouldn't affect the code.

The program is supposed to run forever until the user enters "q", then it will stop running. The try/catch statements are there so that they can only enter integers (except "q" of course).

Any help would be greatly appreciated.

Upvotes: 0

Views: 229

Answers (1)

Lionel Damtew
Lionel Damtew

Reputation: 38

Instead of using the Scanner.nextInt() method you could try using nextLine() to get a String. Then you can check whether that string is equal to "q". If not you can parse the string to an integer with Integer.parseInt(yourString). This could however result in a NumberFormatException, if the user inputs anything other than a number or "q".

while (true) {

            try {
                int randomNumberOne = ThreadLocalRandom.current().nextInt(10, 21); //generates a number between 10 and 20
                int randomNumberTwo = ThreadLocalRandom.current().nextInt(10, 21); //generates a number between 10 and 20
                System.out.println("Type and enter \"q\" to quit at any time \n \n");

                System.out.println(randomNumberOne + " % " + randomNumberTwo + " = ?"); //prints the question

                Scanner userInput = new Scanner(System.in);
                String remainderInputStr = userInput.nextLine();
                if (remainderInputStr.equalsIgnoreCase("q")) {
                    System.exit(0);
                }
                int remainderInput = Integer.parseInt(remainderInputStr);
                if (remainderInput == randomNumberOne % randomNumberTwo) { //if they get the question right
                    userScore += 20; //adds 20 points
                    performance += 1; //adds 1 to the correct question counter
                    performancetotal += 1; //adds 1 to the total question counter
                    System.out.println("Correct answer, Current Score: " + userScore + ", performance: " + performance + "/" + performancetotal + "\n");
                    continue;
                } else { //if they get the question wrong
                    userScore += 0; //adds no points
                    performance += 0; //adds nothing to correct question counter
                    performancetotal += 1;
                    System.out.println("Incorrect answer, Current Score: " + userScore + ", performance: " + performance + "/" + performancetotal + "\n");
                    continue;
                }

            } catch (NumberFormatException e) {
                System.out.println("Invalid input\n");

            }

Upvotes: 1

Related Questions