6

I would like to receive a Double from the user and handle the exception thrown in case the user didn't input a double/int; in that case I'd like to ask the user to insert the amount again. My code gets stuck in a loop if the exception is caught, and keeps printing "Insert amount".

    private static double inputAmount() {
    Scanner input = new Scanner(System.in);
    while (true) {
        System.out.println("Insert amount:");
        try {
            double amount = input.nextDouble();
            return amount;
        }catch (java.util.InputMismatchException e) {continue;}
        }
    }

Thank you in advance.

4
  • It will continue untils he don't write a valid double... what is the problem? Commented Jun 25, 2014 at 16:47
  • 3
    @MarcoAcierno I think the problem is that after an invalid input is answered the program enters an infinite loop that doesn't allow the user to try to input a valid double. Certainly sounds plausible to me. Commented Jun 25, 2014 at 16:48
  • @user3580294 Yes, this is exactly what happens. Commented Jun 25, 2014 at 16:49
  • I think it's because Scanner is blocked at the last double writted by the user. Maybe something like nextLine() will fix it. Commented Jun 25, 2014 at 16:53

4 Answers 4

15

Your program enters an infinite loop when an invalid input is encountered because nextDouble() does not consume invalid tokens. So whatever token that caused the exception will stay there and keep causing an exception to be thrown the next time you try to read a double.

This can be solved by putting a nextLine() or next() call inside the catch block to consume whatever input was causing the exception to be thrown, clearing the input stream and allowing the user to input something again.

Sign up to request clarification or add additional context in comments.

Comments

3

The reason is that it keeps reading the same value over and over, so ending in your catch clause every time.

You can try this:

private static double inputAmount() {
    Scanner input = new Scanner(System.in);
    while (true) {
        System.out.println("Insert amount:");
        try {
            return input.nextDouble();
        }
        catch (java.util.InputMismatchException e) {
            input.nextLine();
        }
    }
}

Comments

0

Its because after the exception is caught, its stays in the buffer of scanner object. Hence its keeps on throwing the exception but since you handle it using continue, it will run in a loop. Try debugging the program for a better view.

Comments

0

This method keeps on executing the same input token until it finds a line separator i.e. input.nextLine() or input.next()

Check out the following code

private static double inputAmount()
{
    Scanner input = new Scanner(System.in);
    while (true)
    {
        System.out.println("Insert amount:");
        try
        {
            double amount = input.nextDouble();
            return amount;
        } 
        catch (java.util.InputMismatchException e)
        {
            input.nextLine();
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.