Results 1 to 4 of 4
Thread: Problem with exception handling
- 10-02-2011, 07:10 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 20
- Rep Power
- 0
Problem with exception handling
Am writing a code to display the sum of 16 numbers taken from user. The program is supposed to print error message, if user input an invalid integer and then let the user to enter that int again, my code is:
The problem is when I enter an invalid integer, program displays error message and than automatically take that same input in next cycle, without letting user to give input. this result in infinite loop displaying error messages on each cycleJava Code:Scanner sc=new Scanner(System.in); int sum=0; for(int i=0; i<16; i++){ System.out.printf("Enter number of rotations for round No %d:\t", i+1); try{ sum += sc.nextInt(); }catch(Exception e){ System.out.printf("\nSorry! invalid number! Enter number again \n"); i--; } } System.out.println(sum);
Their is sample program output
Please help where am wrong :(Java Code:Enter number 1: a Sorry! invalid number! Enter number again Enter number 1: Sorry! invalid number! Enter number again Enter number 1: Sorry! invalid number! Enter number again Enter number 1: Sorry! invalid number! Enter number again Enter number 1: Sorry! invalid number! Enter number again Enter number 1: Sorry! invalid number! Enter number again Enter number 1: Sorry! invalid number! Enter number again Enter number 1: Sorry! invalid number! Enter number again ....................
-
Re: Problem with exception handling
- Don't modify the loop index (i in your program), from within the loop -- it's a recipe for disaster.
- Instead use a while loop inside of your for loop, and change the while condition that allows one to exit the while loop after the user's input has been validated. If the validation fails, that line is never reached and the condition won't be changed until valid input is entered.
- Using a Scanner's nextInt() can be tricky as it does not handle the end of line token very well. If you must use this rather than nextLine() and then Integer.parseInt(...), consider placing sc.nextLine(); in a finally {} block after the catch block.
- 10-02-2011, 07:50 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: Problem with exception handling
This is a little safe integer method I wrote for a lab.
It should help you out a bit without giving away the answer completely.
Java Code:public int safeInt(String message) { int input=0; System.out.println(message); boolean badInput=true; while(badInput){ try{ input=scan.nextInt(); badInput=false; } catch(InputMismatchException e) { System.out.println("That input was not possible.\n please try again."); scan.nextLine(); } } return input; }
- 10-02-2011, 08:01 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
exception handling
By niksipandit in forum New To JavaReplies: 3Last Post: 09-22-2011, 02:25 PM -
Exception Handling
By liljester in forum New To JavaReplies: 4Last Post: 06-21-2010, 03:09 PM -
Exception handling problem
By computerbum in forum New To JavaReplies: 2Last Post: 04-17-2010, 03:15 PM -
Problem with JSP exception handling page
By sidster in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 06-19-2008, 07:28 PM -
Exception Handling...
By focus_nitin in forum New To JavaReplies: 1Last Post: 02-16-2008, 03:13 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks