Trouble exiting from command line program

Hey everyone. I have created a command line to-do list in Java, however, I am struggling to implement the ‘case 0’ which is supposed to exit the program.

Instead of exiting the program, it prints “Goodbye…!” from case0 but then re-prints the displayMenu(). The exit works fine when the user types ‘2’ when prompted if they wish to return to the menu or exit (this prompt occurs every time the user completes a task).

I have a feeling it is to do with either a .nextLine() which has been implemented where it shouldn’t, or there should be an additional one. I thought I had covered all those bases to avoid any skipping but clearly that is still a problem.

Any idea I would love to hear them. Thanks!

public class Main {
    public static void main(String[] args){
        Menu m = new Menu();
        m.startMenu(); // Runs method which displays the options the user can select.
    }
}

    public void setMenu() {
        menu.add("View to-do lists");
        menu.add("Create a new To-Do List");
        menu.add("Add a task");
        menu.add("Delete a task");
        menu.add("Mark task as complete");
        menu.add("Exit");
    }

    public void displayMenu() {
        System.out.println("--------------");
        System.out.println("Main Menu");
        System.out.println("--------------");
        System.out.println("1: " + menu.get(0));
        System.out.println("2: " + menu.get(1));
        System.out.println("3: " + menu.get(2));
        System.out.println("4: " + menu.get(3));
        System.out.println("5: " + menu.get(4));
        System.out.println("0: " + menu.get(5));
        System.out.println();
        System.out.print("Enter an option: ");
    }

   public void setOption() { // User will choose a menu option and this method will execute it.
        int option = -1;
        while (option < 0 || option > 5) {
            try {
                option = input.nextInt(); // User's option input.
                input.nextLine(); // Avoid skipping first nextLine().
            } catch (Exception e) {
                System.out.print("Please enter a valid number! ");
                input.nextLine(); // Avoid skipping first nextLine().
            }
        }

        switch (option) {
             case 1, 2, 3, 4, 5, ....

            case 0: // Exit program.
                exit = true; // Amends boolean expression
                System.out.println("Goodbye....!");
                break;
        }
    }

    public void startMenu() {
        Scanner input = new Scanner(System.in); //Creates input
        int option = -1; //Choice to continue making menu selections
        Menu menu = new Menu(); // Menu object
        menu.setMenu(); //Setting up the menu options -  View A List, Create A List etc.
        do {
            menu.displayMenu();
            menu.setOption(); // Prompts user to select a menu option
            if (exit == false) { // If the user chooses an option other than exit...
                System.out.println(); //... Once they are finished with their selection...
                System.out.println("Return to Menu?"); //... They can return to the main menu...
                System.out.print("1 = Yes/ 2 = No: "); //... Or choose to exit the program.
                option = input.nextInt();
                input.nextLine();
                while (option < 0 || option > 2) {
                    try {
                        option = input.nextInt(); // User's option input.
                        input.nextLine(); // Avoid skipping first nextLine().
                    } catch (Exception e) {
                        System.out.print("Please enter a valid number! ");
                        input.nextLine(); // Avoid skipping first nextLine().
                    }
                }
                if (option == 2) {
                    System.out.println("Goodbye!");
                }
            }
        } while (option != 2); // Keep re-displaying the Menu and Options if user inputs 1.
        input.close(); // Closing the scanner.
    }
}

I guess you could out in a return in that case to exit the function.

Hey @janbazant1107978602 thanks for your comment.
Do you mean add in a return in the ‘Case 0’ code or somewhere within startMenu()?

Thanks!

Yes. Adding a return in

                if (option == 2) {
                    System.out.println("Goodbye!");
                }