0

I have this code that I want to run to solve a problem which needs a three user inputs, and I used Scanner class for this:

public static void main(String[] args) {
    int M = 0;
    int A = 0;
    Scanner input = new Scanner(System.in);
    System.out.println("Please, insert the normal dose in ml:");

    M = input.nextInt();

    System.out.println("Please, insert the set of experiments (3 integers per line, stop by 0 0 0):");
    try {
        while (input.hasNextInt()) {
            System.out.print(input.hasNext());
            int i = input.nextInt();
            A += i;
            System.out.println(A);
        }
    } catch (Exception x) {
        System.out.print(x.getMessage());
    }

    System.out.println("Loop ended");

}

The strange thing is that input.hasNextInt() gets stuck or something after I Insert the three values, It seem that it keeps looping or something even though there are no inputs in the console, can some one provide some help for me?

doubleH90
  • 163
  • 4
  • 17

1 Answers1

0

That's because input.hasNextInt() waits until a integer value is available. It would return false if an alphanumeric value was informed.

You have to define another way to break while loop, maybe with a counter or, like your message says, checking whether 3 values are equal to 0.

andrucz
  • 1,971
  • 2
  • 18
  • 30