Results 1 to 4 of 4
  1. #1
    Kingmaha64 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default 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]);
    }



    }

    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    I suggest full reading the API for Scanner (Java 2 Platform SE 5.0), in particular note the following:
    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.
    In other words, your code will continue to throw an InputMismatchException until the value causing that exception is removed.

  3. #3
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    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");
                }
    
            }

  4. #4
    Kingmaha64 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    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

  1. Try catch loop problems :'(
    By Romally in forum New To Java
    Replies: 7
    Last Post: 11-17-2010, 08:15 PM
  2. try-catch InputMismatchException in a while loop
    By themulator in forum New To Java
    Replies: 17
    Last Post: 10-12-2010, 04:49 AM
  3. Loop inside a switch
    By mustachMan in forum New To Java
    Replies: 3
    Last Post: 02-26-2010, 03:25 AM
  4. Problem printing inside FOR loop
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 05:02 PM
  5. println doesn't print from inside for loop, et.al.
    By rdtindsm in forum New To Java
    Replies: 5
    Last Post: 03-27-2009, 01:19 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •