0

The below program simply reads integers from the console and prints it back. When one enters a non-int (like char or String), the Scanner throws an exception. I tried to handle the exception in the 'try-catch' block and move on to read the next input. After the first non-int input from console, the programs runs into infinite loop. Can someone please help?

public class ScannerTest {
    static int i=1;
    static Scanner sc;
    public static void main (String args[]){
        sc = new Scanner(System.in);
        while (i!=0){
            System.out.println("Enter something");
            go();
        }       
    }   
    private static void go(){
        try{
            i = sc.nextInt();
            System.out.println(i);
        }catch (Exception e){
            System.out.println("Wrong input, try again");
        }               
    }
}
Siva
  • 15
  • 4
  • 1
    Aside of other problems, you need to take user input within your function instead. – devnull Mar 19 '14 at 17:53
  • 1
    possible duplicate of [Infinite While Loop When InputMidmatchException is caught in try-catch block](http://stackoverflow.com/questions/6612806/infinite-while-loop-when-inputmidmatchexception-is-caught-in-try-catch-block) – fgb Mar 19 '14 at 18:02
  • Yes, sorry about that. – Siva Mar 19 '14 at 18:26

3 Answers3

2

When the scanner fails to read the integer, it does not clear the input buffer. So let's say the input buffer contains "abc" because that's what you entered. The call to "nextInt" will fail, but the buffer will still contain "abc". So on the next pass of the loop, the "nextInt" will fail again!

Calling sc.next() in your exception Handler should correct the problem, by removing the incorrect token from the buffer.

Yves Dubois
  • 933
  • 4
  • 7
  • Thank you. Can I please know where did you get this information? I was unable to find in on the Java SE7 documentation online. Anything that I am missing? :) – Siva Mar 19 '14 at 18:17
  • In the Scanner documentation: "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." http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html – David Conrad Mar 19 '14 at 18:21
0

use String :

import java.util.Scanner;

public class ScannerTest {

static int i = 1;
static Scanner sc;

public static void main(String args[]) {
    sc = new Scanner(System.in);
    while (i != 0) {
        System.out.println("Enter something");
        go();
    }
}

private static void go() {
    try {
        i = Integer.parseInt(sc.next());
        System.out.println(i);
    } catch (Exception e) {
        System.out.println("Wrong input, try again");
    }
}
}
AvB
  • 171
  • 1
  • 2
  • 12
-1
As  devnull said take the input from user everytime either in loop or in method,just change the loop to ..and it works fine

1)

 while (i!=0){
         sc = new Scanner(System.in);
        System.out.println("Enter something");
        go();

    } 

2 Other Way

private static void go(){
        try{ sc = new Scanner(System.in);
            i = sc.nextInt();
            System.out.println(i);
        }catch (Exception e){
            System.out.println("Wrong input, try again");
        }               
    }
Gangadhar
  • 11
  • 2