Results 1 to 4 of 4
- 01-08-2011, 12:00 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
try and catch inside a for loop fails!!!
Here Is a Simple code that reads integers via user input using Scanner object and Prints the 5 integer.
The code works fine for all int values. I included a try and catch methods for inputmismatch exception for scanner.nextInt() .
When I enter a character on promt, the code catches the exception but it never scans the next sunsequent values. I tried resetting the scanner object but it doesnt work!
I am a newbie request your assistance and inputs on why the scanner objects fails to take any other inputs after failing(catch exception) the first time or any susequent entries after an exception?.
Enclosing the Code
import java.util.*;
public class TestApp {
public static void main (String[] args){
System.out.println("This is a sort Program");
System.out.println("please enter 5 numbers ");
Scanner sc= new Scanner(System.in);
int [] inputarry = new int[5];
int [] outarry = new int[5];
int i;
for(i=0;i<5;i++){
String S = "Enter Number " + (i+1);
System.out.println(S);
try {
sc.reset();
inputarry[i] = sc.nextInt();
}
catch (InputMismatchException e){
System.out.println("only ineger value");
}
}
System.out.println("The items before sort");
for(i=0;i<5;i++){
System.out.print('\t');
System.out.print(inputarry[i]);
}
}
}
- 01-08-2011, 01:06 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
I suggest full reading the API for Scanner (Java 2 Platform SE 5.0), in particular note the following:
In other words, your code will continue to throw an InputMismatchException until the value causing that exception is removed.When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.
- 01-08-2011, 01:07 AM #3
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
you can use an infinity loop
Java Code:String input=""; while( true ){ String S = "Enter Number " + (i+1); System.out.println(S); input = sc.next(); try { inputarry[i] = Integer.parseInt( input ); if(i==4) break; i++; } catch (NumberFormatException e){ System.out.println("only ineger value"); } }
- 01-08-2011, 02:40 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
Thank You javahater and dowhile!!!
Wow scanner never passes the token on input mismatch interesting!!!
I have fixed the code in a crude way but! it works and I have better clarity now thanks!
import java.util.*;
public class Testscan {
public static void main (String[] args){
System.out.println("This is a sort Program");
System.out.println("please enter 5 numbers ");
Scanner sc= new Scanner(System.in);
int [] inputarry = new int[5];
int [] outarry = new int[5];
int i;
for(i=0;i<5;i++){
String S = "Enter Number " + (i+1);
System.out.println(S);
try {
sc.reset();
inputarry[i] = sc.nextInt();
}
catch (InputMismatchException e){
System.out.println("only int value");
sc.next(); // crude fix
i--; // crude fix
}
}
System.out.println("The items before sort");
for(i=0;i<5;i++){
System.out.print('\t');
System.out.print(inputarry[i]);
}
}
}
Similar Threads
-
Try catch loop problems :'(
By Romally in forum New To JavaReplies: 7Last Post: 11-17-2010, 08:15 PM -
try-catch InputMismatchException in a while loop
By themulator in forum New To JavaReplies: 17Last Post: 10-12-2010, 04:49 AM -
Loop inside a switch
By mustachMan in forum New To JavaReplies: 3Last Post: 02-26-2010, 03:25 AM -
Problem printing inside FOR loop
By cassysumandak in forum New To JavaReplies: 1Last Post: 10-04-2009, 05:02 PM -
println doesn't print from inside for loop, et.al.
By rdtindsm in forum New To JavaReplies: 5Last Post: 03-27-2009, 01:19 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks