JAVA: code for menu selection using Scanner class

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 7
Reputation: Maria5683 is an unknown quantity at this point 
Solved Threads: 0
Maria5683 Maria5683 is offline Offline
Newbie Poster

JAVA: code for menu selection using Scanner class

 
0
  #1
Nov 9th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: JAVA: code for menu selection using Scanner class

 
0
  #2
Nov 9th, 2004
Something along these lines:
  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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 7
Reputation: Maria5683 is an unknown quantity at this point 
Solved Threads: 0
Maria5683 Maria5683 is offline Offline
Newbie Poster

Re: JAVA: code for menu selection using Scanner class

 
0
  #3
Nov 10th, 2004
Thank you. This help out a lot!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: vgi is an unknown quantity at this point 
Solved Threads: 0
vgi vgi is offline Offline
Newbie Poster
 
0
  #4
Oct 19th, 2009
How do you write the code that allows you to go back to the menu after choosing an option?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 48
Reputation: sneaker is an unknown quantity at this point 
Solved Threads: 7
sneaker sneaker is offline Offline
Light Poster
 
0
  #5
Oct 19th, 2009
Originally Posted by vgi View Post
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: vgi is an unknown quantity at this point 
Solved Threads: 0
vgi vgi is offline Offline
Newbie Poster
 
0
  #6
Oct 20th, 2009
thanks alott! it really helpedd
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC