How do I get get the menu to display again after the selected method executes?
I have the menu option print to the console. Then it takes user input (1 - 6), calls according method, and then should return to the menu for the user to select from the menu again.
After the selected method executes, the program just ends.
public static void main (String[] arg) {
Scanner kbd = new Scanner(System.in);
String mainMenu = ("Select a choice from the menu: \n"
+ "1. Add new DVD entry\n"
+ "2. Look Up DVD Entry\n"
+ "3. Display DVDs By Category\n"
+ "4. Remove DVD Entry\n"
+ "5. Save Data\n"
+ "6. Exit");
System.out.println(mainMenu);
menuChoice = kbd.nextInt();
while (menuChoice < 1 || menuChoice > 6) {
System.out.print("\nError! Incorrect choice.\n");
System.out.println(mainMenu);
menuChoice = kbd.nextInt();
}
switch (menuChoice) {
case 1: {
// method code
}
else {
// method code
return;
}
}
case 2: {
// method code
return;
}
case 3: {
// method code
return;
}
case 4: {
// method code
return;
}
case 5: {
// method code
return;
}
case 6: {
// method code
System.exit(0);
return;
}
}
}
Use a do while
You also have to remove the
return
from yourcase
s. Otherwise it will return out of main. Replace them withbreak
;This is how switch cases need to be
You don't usually need
{
orif
inside the switch case.wrap your code inside