0

I'm trying to figure out why my program is skipping a user input so I can come up with a solution. If I intentionally give it bad input and cause the InputMismatchException to be thrown, it skips the inputDouble = in.nextDouble(); line. Here is what the output looks like:

Please enter floating point value or -1 to stop
15.7
Please enter floating point value or -1 to stop
15.7
Please enter floating point value or -1 to stop
r
Number format is incorrect please try again
Please enter floating point value or -1 to stop
Number format is incorrect please try again
Total is: 31.4

And here is my code

import java.util.InputMismatchException;
import java.util.Scanner;

public class AddingNumbers {
    public static void main(final String[] args) {
        Scanner in = new Scanner(System.in);
        double inputDouble = 0;
        double total = 0;
        int tries = 0;
        boolean done = false;

        while (!done) {
            if (tries < 2) {
                try {
                    System.out.println("Please enter floating point value or -1 to stop");
                    inputDouble = in.nextDouble();

                    if (inputDouble != -1) {
                        total = total + inputDouble;
                    } else {
                        System.out.println("Total is: " + total);
                        done = true;
                    }
                } catch (InputMismatchException exception) {
                    System.out.println("Number format is incorrect please try again");
                    tries++;
                }
            } else {
                System.out.println("Total is: " + total);
                done = true;
            }
        }

        in.close();
    }
}
Dennis Meng
  • 4,932
  • 14
  • 30
  • 36
  • What environment? Is this inside an IDE like NetBeans or Eclipse? Some IDEs have odd buffering issues that make it look like your program is reading a line behind. Please let us know *exactly* where and how you're doing this input typing. – markspace Nov 03 '15 at 03:03
  • Sorry it's Eclipse. Changing it to inputFloat = new Scanner(System.in).nextFloat(); fixed it however now I have an error for a resource leak. Is there another way to close it besides in.close() ? – Garrett Penfield Nov 03 '15 at 03:20
  • I edited my answer to add some additional information. If it helped solve your problem consider selecting it as the answer. – Alexander Nov 03 '15 at 11:36

2 Answers2

0

You could try to put a blank in.nextLine(); right below your inputDouble=in.nextDouble();

Kithelas
  • 48
  • 7
  • No luck I got the same result: Please enter floating point value or -1 to stop 15.7 Please enter floating point value or -1 to stop 15.7 Please enter floating point value or -1 to stop 15.7 Please enter floating point value or -1 to stop r Number format is incorrect please try again Please enter floating point value or -1 to stop Number format is incorrect please try again Total is: 47.09999942779541 – Garrett Penfield Nov 03 '15 at 03:49
0

The Scanner still has invalid input in the buffer. To "clear" the buffer you need to use in.nextLine();. Use in.nextLine(); after tries++ to solve your problem.

Alexander
  • 480
  • 2
  • 5
  • 21