473,320 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Basic Menu prompts in Java

New to Java. How best to deal with unexpected input from a menu prompt, e.g, A menu has 3 options, 1,2,3, typing 3 quits the program. However, how do you deal with anything else that may be entered? If 3.7 or JFJJfor any other incorrect input is entered how to not have the program quit with an exception error but to gracefully output an error message( this I can do) and re prompt for valid input?
Mar 6 '07 #1
2 1993
horace1
1,510 Expert 1GB
assuming you are using scanner you can do something like this
Expand|Select|Wrap|Line Numbers
  1.   Scanner in = new Scanner ( System.in );
  2.   while(true)                      // repeat until valid selection made
  3.     {
  4.     System.out.println ("select from\n  1) Option 1\n  2) Option 2\n  3) Option 3" );
  5.     System.out.print ( "Selection: " );
  6.     if(in.hasNextInt())            // if int entered
  7.       switch ( in.nextInt() ) 
  8.         {
  9.         case 1:
  10.           System.out.println ( "You picked option 1" );
  11.           return 1;
  12.         case 2:
  13.           System.out.println ( "You picked option 2" );
  14.           return 2;
  15.         case 3:
  16.           System.out.println ( "You picked option 3" );
  17.           return 3;
  18.         default:
  19.         }
  20.     // arrives here if invalid input is entered
  21.     in.nextLine();                 // skip any faulty input
  22.     System.err.println ( "Unrecognized option" );
  23.     }
  24.  
it will reject any input that is not 1 2 or 3
Mar 6 '07 #2
Thanks for this- I will try when I'm near my compiler- I'll need to expand on it but I think if I can get the logic of this flow, I'll be able to move onto a bit more complexity, GUI prompts etc.Thanks again.
Mar 6 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: CHANGE username to westes | last post by:
What are the most popular, and well supported, libraries of drivers for bar code scanners that include a Visual Basic and C/C++ API? My requirements are: - Must allow an application to be...
10
by: John Ortt | last post by:
Hi Everyone, I have created a Javascript menu for my site which uses frames. The first stage loads fine but I want two drill down menus ("About Me Menu" and "Projects Menu"). The pages load...
5
by: Lee David | last post by:
I went to the sun site and downloaded what I hope is the development part of java. I downloaded JDK5 with Netbeans. I installed it and now have a folder in my program group "Netbeans". Is that...
5
by: pankaj_wolfhunter | last post by:
Greetings, When i log onto sql server using tsql i get the following line of information: locale is...
1
by: jennifer.lyell | last post by:
Hello, I am using Frontpage 2003 for a site design. I have 2 .swf files on the page as well as a menu bar (with cascading menus- created with All Web Menus Pro) that are on the page. When I...
9
by: aleplgr | last post by:
Hi! I'm trying to let the end-user select the language of the menues I'm showing him. To do that I've got this Selector class that shows the end user a combo box, a label and a button. In the combo...
1
by: Kayvine | last post by:
Hi guys, this is a question I have for an assignment, it is pretty long, but I am not asking for the code(well if someone wants to write I'll be really happy, lol), but I just want to know how to...
14
by: MartinRinehart | last post by:
Working on parser for my language, I see that all classes (Token, Production, Statement, ...) have one thing in common. They all maintain start and stop positions in the source text. So it seems...
1
by: whitep8 | last post by:
Hi all, im attempting to make a user menu, which the user instigates the exit, not netbeans. basically i want a menu displayed, user makes selection, that specific piece is ran, on...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.