-1

I have been looking at this for too long and have tried many things to stop this program from throwing an error or ignoring the program stop code I have tried running.

I want to have an option that stops the program from running if a user enters a number, in this case 3, but I can't find any thing that I can stop a program with. I tried "keyboard.exit(1);" and "System.stop(1);". I just want to accomplish this without needing to restructure the entire methods system.

Scyfer
  • 13
  • 1
  • 4
  • Does this help you? [System.exit()](https://stackoverflow.com/questions/3715967/when-should-we-call-system-exit-in-java) – Kirit Feb 07 '21 at 06:16

2 Answers2

0

You can use below line to terminate your program in your if condition where you are checking entered value is 3 or not.

System.exit(0);

Satyajit Bhatt
  • 211
  • 1
  • 7
  • 2
    Of course, the other option is to arrange things so that when the user types the magic "stop" command, the program's flow of control naturally bypasses all further actions and eventually reaches the end of the `main` method. But sometimes it's just so much simpler to use `System.exit(0)`!. – Kevin Anderson Feb 07 '21 at 07:37
0

First, there's an issue in the do-while loop in getSystemChoice which exits only when user selects options 1 or 2.

Thus, the logic of this loop/method should be fixed:

public static int getSystemChoice() {
    Scanner input = new Scanner(System.in);  //create scanner
    int systemChoice;
    do {
        System.out.println("If you are using the Metric system, please enter a 1.");
        System.out.println("If you are using the Imperial system, please enter a 2.");
        System.out.println("To quit the program, please enter a 3.");

        systemChoice = input.nextInt();
        // exit here if 3 is selected
        if (systemChoice == 3) {
            System.exit(0);
        }
    }
    while (systemChoice != 1 && systemChoice != 2);

    return systemChoice;
}

However, this method may still need improvement to avoid repeated printing of the same menu and print a message about invalid input instead.

Also, the existing code will throw an InputMismatchException if a non-integer value is entered, so it may be better to use input.next() to read any token from the input as a String.

public static int getSystemChoice() {
    Scanner input = new Scanner(System.in);  //create scanner
    // print menu once
    System.out.println("If you are using the Metric system, please enter a 1.");
    System.out.println("If you are using the Imperial system, please enter a 2.");
    System.out.println("To quit the program, please enter a 3.");
    
    while (true) {
        String systemChoice = input.next(); // read any token from the input as String
        switch (systemChoice) {
            case "1": case "2":
                return Integer.parseInt(systemChoice);
            case "3":
                System.out.println("Bye!"); // optional message to indicate exit
                System.exit(0);
            default:
                System.out.println("Invalid input, please enter 1, 2, or 3");
        }
    }    
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42