Search code examples
javamenuswitch-statementcase

How to return to switch case menu after executing method?


I am currently writing a program that does simple arithmetic functions from a menu. Ideally I would like it to execute the method chosen from the case, and then return to the menu upon completion. Instead, when this is run it just ends the program after reaching the end of the method.

public static void main(String[] args) {
        String menu = ("Please choose one option from the following menu: \n"
        + "1. Calculate the sum of integers 1 to m\n"
        + "2. Calculate the factorial of a given number\n"
        + "3. Calculate the amount of odd integers in a given sequence\n"
        + "4. Display the leftmost digit of a given number\n"
        + "5. Calculate the greatest common divisor of two given integers\n"
        + "6. Quit\n");
       
        Scanner console = new Scanner(System.in);
        do {

            System.out.println(menu);
            int selection = console.nextInt();

            switch (selection) {
                case 1:
                sumOfIntegers();
                break;

                case 2:
                factorialOfNumber();
                break;

                case 3:
                calcOddIntegers();
                break;

                case 4:
                displayLeftDigit();
                break;

                case 5:
                greatestCommonDivisor();
                break;

                case 6:
                System.out.println("Bye");
                break;
                
            }
        } while (selection != 6);
            console.close();
    }

I need after a menu option is selected and the method completed for it to return to the switch case menu. I have tried a do while loop from another thread but that did not work for me. I probably did something wrong and am very new to coding. Any help is appreciated.

Edit: The error that I receive is a NoSuchElementException on the line where it takes another input from the scanner.


Solution

  • The problem is that you're using a variable named selection that is defined inside the loop, so it is not accessible outside of it. To fix this, you should declare the variable selection before the loop, and then check the value of selection inside the loop's condition.

    public static void main(String[] args) {
        String menu = ("Please choose one option from the following menu: \n"
        + "1. Calculate the sum of integers 1 to m\n"
        + "2. Calculate the factorial of a given number\n"
        + "3. Calculate the amount of odd integers in a given sequence\n"
        + "4. Display the leftmost digit of a given number\n"
        + "5. Calculate the greatest common divisor of two given integers\n"
        + "6. Quit\n");
    
        Scanner console = new Scanner(System.in);
        int selection = 0;
        do {
            System.out.println(menu);
            selection = console.nextInt();
    
            switch (selection) {
                case 1:
                    sumOfIntegers();
                    break;
                case 2:
                    factorialOfNumber();
                    break;
                case 3:
                    calcOddIntegers();
                    break;
                case 4:
                    displayLeftDigit();
                    break;
                case 5:
                    greatestCommonDivisor();
                    break;
                case 6:
                    System.out.println("Bye");
                    break;
            }
        } while (selection != 6);
        console.close();
    }
    

    With this modification, the program will execute the method chosen from the case and then return to the menu upon completion.