1

I had setup a method to check if the input matches the type int and doesn't return until it does.

It worked fine for a couple of projects so far but when I used it in my current one I got this kind of error when going into the method for the second time around.

Exception in thread "main" java.util.NoSuchElementException

Here is the method:

private static int inputHandler() {
    Scanner sc = new Scanner(System.in);
    int num = 0;

    try{
        System.out.println("Enter a number:");
        num = sc.nextInt();
    } catch(InputMismatchException e){
        System.out.println("Input must match type int");
        num = inputHandler();
    }
    sc.close();
    return num;
} 

If anyone knows how to fix this issue I would appreciate the help.

  • Welcome to Stack Overflow! Please take the [tour](/tour), have a look around, and read through the [help center](/help), in particular [How do I ask a good question?](/help/how-to-ask) and [What topics can I ask about here?](https://stackoverflow.com/help/on-topic). Please add the **complete error message** and a [Minimal, Complete, and Verifiable example](/help/mcve). – Timothy Truckle Dec 29 '18 at 18:36

0 Answers0