954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

JAVA: code for menu selection using Scanner class

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

Maria5683
Newbie Poster
7 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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();
  }
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thank you. This help out a lot!

Maria5683
Newbie Poster
7 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

How do you write the code that allows you to go back to the menu after choosing an option?

vgi
Newbie Poster
2 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 
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 :)

sneaker
Junior Poster in Training
Team Colleague
77 posts since Jul 2009
Reputation Points: 40
Solved Threads: 13
 

thanks alott! it really helpedd :D

vgi
Newbie Poster
2 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Hi, I had a public void aldready. How do I create a new public void so that I can do the menu?

fairy1992224
Newbie Poster
17 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You