943,483 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 18430
  • Java RSS
Nov 9th, 2004
-2

JAVA: code for menu selection using Scanner class

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Maria5683 is offline Offline
7 posts
since Nov 2004
Nov 9th, 2004
0

Re: JAVA: code for menu selection using Scanner class

Something along these lines:
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. class Main {
  4. public void display_menu() {
  5. System.out.println ( "1) Option 1\n2) Option 2\n3) Option 3" );
  6. System.out.print ( "Selection: " );
  7. }
  8.  
  9. public Main() {
  10. Scanner in = new Scanner ( System.in );
  11.  
  12. display_menu();
  13. switch ( in.nextInt() ) {
  14. case 1:
  15. System.out.println ( "You picked option 1" );
  16. break;
  17. case 2:
  18. System.out.println ( "You picked option 2" );
  19. break;
  20. case 3:
  21. System.out.println ( "You picked option 3" );
  22. break;
  23. default:
  24. System.err.println ( "Unrecognized option" );
  25. break;
  26. }
  27. }
  28.  
  29. public static void main ( String[] args ) {
  30. new Main();
  31. }
  32. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 10th, 2004
0

Re: JAVA: code for menu selection using Scanner class

Thank you. This help out a lot!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Maria5683 is offline Offline
7 posts
since Nov 2004
Oct 19th, 2009
0
Re: JAVA: code for menu selection using Scanner class
How do you write the code that allows you to go back to the menu after choosing an option?
vgi
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vgi is offline Offline
2 posts
since Oct 2009
Oct 19th, 2009
0
Re: JAVA: code for menu selection using Scanner class
Click to Expand / Collapse  Quote originally posted by vgi ...
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.
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. public class InputMenu
  4. {
  5. public void display_menu()
  6. {
  7. System.out.println("1) Option 1\n2) Option 2\n3) Option 3");
  8. System.out.print("Selection: ");
  9. }
  10.  
  11. public void question()
  12. {
  13. System.out.println("Would you like to proceed or quit?");
  14. System.out.println("To proceed enter 9.");
  15. System.out.println("If you wish to quit enter 0.");
  16.  
  17. Scanner q = new Scanner(System.in);
  18.  
  19. switch (q.nextInt())
  20. {
  21. case 0:
  22. System.out.println ("Thank you and godbye.");
  23. break;
  24.  
  25. case 9:
  26. System.out.println ("Please proceed.");
  27. new InputMenu();
  28. break;
  29.  
  30. default:
  31. System.err.println ( "Unrecognized option" );
  32. break;
  33. }
  34. }
  35.  
  36. public InputMenu()
  37. {
  38. Scanner in = new Scanner(System.in);
  39. display_menu();
  40.  
  41. switch (in.nextInt())
  42. {
  43. case 1:
  44. System.out.println ( "You picked option 1" );
  45. question();
  46. break;
  47.  
  48. case 2:
  49. System.out.println ( "You picked option 2" );
  50. question();
  51. break;
  52.  
  53. case 3:
  54. System.out.println ( "You picked option 3" );
  55. question();
  56. break;
  57.  
  58. default:
  59. System.err.println ( "Unrecognized option" );
  60. break;
  61. }
  62. }
  63.  
  64. public static void main (String[]args)
  65. {
  66. new InputMenu();
  67. }
  68. }
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
Last edited by sneaker; Oct 19th, 2009 at 5:44 pm.
Sponsor
Reputation Points: 40
Solved Threads: 13
Junior Poster in Training
sneaker is offline Offline
76 posts
since Jul 2009
Oct 20th, 2009
0
Re: JAVA: code for menu selection using Scanner class
thanks alott! it really helpedd
vgi
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vgi is offline Offline
2 posts
since Oct 2009
Jan 8th, 2011
0
Re: JAVA: code for menu selection using Scanner class
Hi, I had a public void aldready. How do I create a new public void so that I can do the menu?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fairy1992224 is offline Offline
17 posts
since Dec 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: JAVA CODE <<<need your help
Next Thread in Java Forum Timeline: Download a file from remote server to local machine using java program by invoking sh





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC