Results 1 to 2 of 2
- 06-05-2011, 07:50 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 1
- Rep Power
- 0
InputMismatch Exception inside do while menu, please help!
Hello people, I'm newbie to java and wants to know where did I got wrong in my code to print the do while menu, the option available is from 1-9 and it's int, when i input string it catch the InputMismatch exception, my code has caught it but here got some problems.
boolean menu = false;
int option = 0;
do{
System.out.println("\t \t MENU \n \n");
System.out.println("\t 1. aaa \n");
System.out.println("\t 2. bbb \n");
System.out.println("\t 3. ccc \n");
System.out.println("\t 4. ddd \n");
System.out.println("\t 5. eee \n");
System.out.println("\t 6. fff \n");
System.out.println("\t 7. ggg \n");
System.out.println("\t 8. hhh \n");
System.out.println("\t 9. iii \n");
while (!menu){
System.out.print("\t Enter Your Option : ");
try{
option = console.nextInt();
menu = true;
}
catch ( InputMismatchException ex )
{
System.out.println();
System.out.println("\t Wrong input, option valid is from 1-9" );
System.out.println();
String flush = console.next();
}
}
if (option == 1){
System.out.print("\t Enter Name : ");
String name = console.next(".*");
System.out.println("");
System.out.println("Name : "+name);
}
.
.
.
.
.
.
.
else if (option == 9){
System.out.print("\t Byebye ");
break;
}
}
while(option != 9);
When I input option (String) it can catch the exception and ask for option again, but when I input option 1, and write the name (String) it print the menu again, but then it ask for name input, not the option input, how to solve the problem??thank you so much for your guide :D
- 06-05-2011, 08:32 PM #2
please copy and paste here the full error message.it catch the InputMismatch exception
If you can include everything shown on the console so we can see what was entered.
Add a call to printStackTrace() to all the catch blocks in your code.
When you post some code please surround the code in code tags. See: http://www.java-forums.org/misc.php?do=bbcode#code
Also a full program that compiles and executes makes it easier for anyone to test it.Last edited by Norm; 06-05-2011 at 08:35 PM.
Similar Threads
-
Menu Type cast Exception
By arulmozs in forum AWT / SwingReplies: 8Last Post: 11-02-2009, 10:34 PM -
no menu, no exception
By cdene in forum New To JavaReplies: 0Last Post: 10-02-2009, 01:50 AM -
Fill a menu dynamically when menu is shown
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:47 PM -
React to menu action and checkbox menu
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:50 PM -
how to create Popup Menu with Sub Menu while right-clicking the JTree Node??
By Kabiraa in forum AWT / SwingReplies: 7Last Post: 05-09-2008, 07:54 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks