| | |
JAVA: code for menu selection using Scanner class
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Something along these lines:
Java Syntax (Toggle Plain Text)
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(); } }
I'm here to prove you wrong.
•
•
Join Date: Jul 2009
Posts: 48
Reputation:
Solved Threads: 7
0
#5 Oct 19th, 2009
•
•
•
•
How do you write the code that allows you to go back to the menu after choosing an option?
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)
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(); } }
Hope this gave you an idea of how it works
Last edited by sneaker; Oct 19th, 2009 at 5:44 pm.
![]() |
Similar Threads
- Simple Scanner Class Question (Java)
- Can't find Scanner class? (Java)
- I Hope Someone Who Is Online Will Help......Java Code (Java)
- Question about the Scanner class (Java)
Other Threads in the Java Forum
- Previous Thread: How to identify operators in a string?
- Next Thread: Help with Java Address Book
| Thread Tools | Search this Thread |
-xlint add android api applet application array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component converter database digit eclipse equation error event exception fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying oracle page pearl print problem program programming project qt recursion scanner screen scrollbar server set size sms sort spamblocker sql string swing system thread threads time tree variablebinding windows xor






