breadfan18
breadfan18

Reputation: 49

While loop randomly looping.Can't figure out why.

Java noob here. I'm trying to write an "Applications Menu" program that gives the users a main menu of different applications they can run. Once they make the selection, that app runs and then gives them an option to Go back to main menu, or to Quit the program. I've run into an issue here that if the user runs several applications by repeatedly going back to the main menu, when they eventually select the option to quit the program, they get prompted to make the quit selection the same number of times that the user ran different applications. Meaning, if during the run, they run applications 4 times, they get re-promted with the "1 to Return to Main Menu or 2 to Quit" prompt 4 times. Help??

public class Menu {
    private MathPractice math = new MathPractice();
    private Scanner scan = new Scanner(System.in);
    private Calculator calc = new Calculator();
    private BasePage base = new BasePage();
    private String menuHeader = "Welcome to the Math Applications Menu";

    public void mainMenu()
    {
        while (true){
            System.out.println();
            base.headerUnderline(menuHeader);
            System.out.println(menuHeader);
            base.headerUnderline(menuHeader);
            System.out.println("Enter 1 for Bill Pay");
            System.out.println("Enter 2 for Multiplication Table");
            System.out.println("Enter 3 for Test Score");
            System.out.println("Enter 4 for Calculator");
            base.headerUnderline(menuHeader);

            int userChoice = scan.nextInt();

            if (userChoice == 1)
            {
                math.billPay();
                postAppMenu();
                break;
            }
            else if (userChoice == 2)
            {
                math.multiplicationTable();
                postAppMenu();
                break;
            }
            else if (userChoice == 3)
            {
                math.testScore();
                postAppMenu();
                break;
            }
            else if (userChoice == 4)
            {
                calc.calculator();
                postAppMenu();
                break;
            }
            else {
                System.out.println("Please select a valid option!");
            }
        }
    }

    private void postAppMenu()
    {
        while (true){
            base.headerUnderline(menuHeader);
            System.out.println("Enter 1 for Main Menu");
            System.out.println("Enter 2 to Quit");
            base.headerUnderline(menuHeader);

            int userChoice = scan.nextInt();

            if (userChoice == 1){
                mainMenu();
            }
            else if (userChoice == 2){
                System.out.println("Thank your for using the Math Application.");
                System.out.println("Have a good day. Goodbye.");
                break;
            }else {
                System.out.println("That's not a valid selection");
            }
        }

    }
}

Upvotes: 0

Views: 56

Answers (2)

UncaAlby
UncaAlby

Reputation: 5354

You are calling mainMenu from postAppMenu. Without sitting here and doing a complete static analysis, what I think is happening is every time you enter postAppMenu, you call mainMenu again, which means you're getting deeper into the function stack. When you finally decide to Quit, it has to roll back that stack one level at a time.

Try a rewrite where, instead of calling mainMenu from postAppMenu, you just have postAppMenu "return" back to mainMenu.

Unless you intend to implement a recursive algorithm of some sort, it generally is not a good idea for Method A to call Method B which calls Method A. It can be done, but it can be tricky.

Upvotes: 2

radcore
radcore

Reputation: 339

What happens if you break from your postAppMenu method after calling the mainMenu?

    if (userChoice == 1){
        mainMenu();
        break;
    }

Upvotes: 1

Related Questions