So I am writing a menu based program, and I am stuck in one part. Here's my code:
public static void main(String [] args) throws FileNotFoundException {
switch (menu()) {
case 1:
System.out.println("Stub 1");
menu();
break;
case 2:
System.out.println("Stub 2");
menu();
break;
case 3:
System.out.println("Stub 3");
menu();
break;
case 4:
System.out.println("Program Terminated");
break;
}
}
public static int menu() {
System.out.println("Choose a task number from the following: ");
System.out.println("\t1. - See histogram of name's popularity");
System.out.println("\t2. - Compare two names in a specific decade");
System.out.println("\t3. - Show what name had a specific rank for a certain decade");
System.out.println("\t4. - Exit program");
int opt = 0;
int option = getInt(input,"Enter number (1-4): ", 1, 4);
if (option == 1) {
opt = 1;
}
else if (option == 2) {
opt = 2;
}
else if (option == 3) {
opt = 3;
}
else {
opt = 4;
}
return opt;
}
My question is, how can I get the menu to 'reset' after an option is pressed. For example, I choose 1, the program performs the action and after it's done, it shows the options menu again until I press 4 to terminate it.
The getInt
method in my code simply returns an int between 1 and 4.