How do you write the code using Scanner class to set up a menu (i.e 1 for something, 2 for something, and so on.) Then have the user to choose.
Thanks
A simple menu is easiest to manage with one Scanner on System.in and a loop that keeps prompting until the user chooses to exit. Avoid creating multiple Scanners on System.in or recursively constructing new menu objects to "go back"; both can cause resource issues and hard-to-trace bugs. Also be careful with nextInt() because it leaves the newline in the buffer; reading whole lines with nextLine() and then parsing is more predictable. Do not close the Scanner tied to System.in; just let the program end.
Example structure you can drop into your program (separate methods keep things tidy and reusable):
import java.util.Scanner;
public class MenuApp {
public static void main(String[] args) {
Scanner in = new Scanner(System.in); // do not close System.in
runMenu(in);
}
static void runMenu(Scanner in) {
int choice = -1;
while (choice != 0) {
printMenu();
String line = in.nextLine().trim();
try {
choice = Integer.parseInt(line);
} catch (NumberFormatException e) {
System.out.println("Please enter a number like 0, 1, or 2.");
continue;
}
switch (choice) {
case 1: handleOption1(); break;
case 2: handleOption2(); break;
case 0: System.out.println("Goodbye!"); break;
default: System.out.println("Unknown option.");
}
}
}
static void printMenu() {
System.out.println("0) Exit");
System.out.println("1) Do thing A");
System.out.println("2) Do thing B");
System.out.print("Choose: ");
}
static void handleOption1() { /* ... */ }
static void handleOption2() { /* ... */ }
} If you already have a public method, you can add another (for example public void runMenu(...)) in the same class; just remember only one public top-level class per file. See the official docs for Scanner and numeric parsing: Scanner (Java SE 21).
Jump to Post— Narue 5,707Something along these lines:
import java.util.*; class Main { public void display_menu() { System.out.println ( "1) Option 1\n2) Option 2\n3) Option 3" ); System.out.print ( "Selection: " ); } public Main() { Scanner in = new Scanner ( System.in ); display_menu(); switch ( in.nextInt() ) { case …
Jump to Post— sneaker 25How do you write the code that allows you to go back to the menu after choosing an option?
Hi
You can make it simple an add a new method like in this example below that is based on the class above. I have chanced the name of the …
Something along these lines:
import java.util.*;
class Main {
public void display_menu() {
System.out.println ( "1) Option 1\n2) Option 2\n3) Option 3" );
System.out.print ( "Selection: " );
}
public Main() {
Scanner in = new Scanner ( System.in );
display_menu();
switch ( in.nextInt() ) {
case 1:
System.out.println ( "You picked option 1" );
break;
case 2:
System.out.println ( "You picked option 2" );
break;
case 3:
System.out.println ( "You picked option 3" );
break;
default:
System.err.println ( "Unrecognized option" );
break;
}
}
public static void main ( String[] args ) {
new Main();
}
} Thank you. This help out a lot!
How do you write the code that allows you to go back to the menu after choosing an option?
How do you write the code that allows you to go back to the menu after choosing an option?
Hi
You can make it simple an add a new method like in this example below that is based on the class above. I have chanced the name of the class because I personally do not think that classes should be named as Main.
import java.util.*;
public class InputMenu
{
public void display_menu()
{
System.out.println("1) Option 1\n2) Option 2\n3) Option 3");
System.out.print("Selection: ");
}
public void question()
{
System.out.println("Would you like to proceed or quit?");
System.out.println("To proceed enter 9.");
System.out.println("If you wish to quit enter 0.");
Scanner q = new Scanner(System.in);
switch (q.nextInt())
{
case 0:
System.out.println ("Thank you and godbye.");
break;
case 9:
System.out.println ("Please proceed.");
new InputMenu();
break;
default:
System.err.println ( "Unrecognized option" );
break;
}
}
public InputMenu()
{
Scanner in = new Scanner(System.in);
display_menu();
switch (in.nextInt())
{
case 1:
System.out.println ( "You picked option 1" );
question();
break;
case 2:
System.out.println ( "You picked option 2" );
question();
break;
case 3:
System.out.println ( "You picked option 3" );
question();
break;
default:
System.err.println ( "Unrecognized option" );
break;
}
}
public static void main (String[]args)
{
new InputMenu();
}
} As you see a new method question() is added and this method is called from all cases in the switch() (inside InputMenu() ) except the default case.
Hope this gave you an idea of how it works :)
thanks alott! it really helpedd :D
Hi, I had a public void aldready. How do I create a new public void so that I can do the menu?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.