Trying to create a loop for the user to press enter to return to main menu
I am having trouble with returning to the main menu.
public static void main(String[] args)
{
Project4 app = new Project4();
//Display all Menu options
app.displayMenus();
//Asks user to pick a menu option
System.out.println("Please choose a menu option:");
int option = keyboard.nextInt();
while(option >=1 || option <= 5)
{
if(option == 1)
{
app.menu1();
app.displayMenus();
System.out.println("Please choose a menu option:");
option = keyboard.nextInt();
}
if(option == 2)
{
app.menu2();
app.displayMenus();
System.out.println("Please choose a menu option:");
option = keyboard.nextInt();
}
else if(option == 3)
{
app.menu3();
System.out.println("Press enter to return to the menu.");
String enter = keyboard.nextLine();
}
So the statement else if(option == 3) is supposed to display my menu3() method which displays information to the user. I am trying to get it to press enter to return to the main menu which is called with the method app.displayMenus(). I have tried the loops, but I just don't know what to put after String enter = keyboard.nextLine() to get it to the main menu and start the process over again. Any help would be appreciated. Thanks!
Re: Trying to create a loop for the user to press enter to return to main menu
Sounds like a job for a while loop?
Re: Trying to create a loop for the user to press enter to return to main menu
I don't know how to write it though because the else if statement before it confuses me on what to put in the while loops boolean expression
else if(option == 3)
{
while(?)
{
app.menu3()
System.out.println("Press enter to return to the main menu");
String enter = keyboard.nextLine();
}
then do I put the main menu method after it or what?
Re: Trying to create a loop for the user to press enter to return to main menu
What exactly do you want to happen when the user presses enter after pressing 3?
Re: Trying to create a loop for the user to press enter to return to main menu
when they press enter it should return them to the main menu to press a number again. The main menu method is app.displayMenus();
Re: Trying to create a loop for the user to press enter to return to main menu
It still seems like a while or a do while is your best option. Just save what the user enters and start over (maybe use the continue statement).
If you want more help, you'll have to provide an SSCCE that we can actually run- just take out the secondary menu methods, but have the program flow be the same with a clearly stated goal of how you want to change it.