User Inputs During a while loop; Stops/Blocks While loop - Java

185 Views Asked by At

I am trying to stop a while loop w/user input in java. I am utilizing the scanner utility. However, the scanner stops the loop from progressing. How can I make it so that the code enables user input at any time without blocking the loop?

CODE

public static void stopwatch() {
            System.out.println("When you are ready, please enter '2'.");
            Scanner reply = new Scanner(System.in);
            String two = reply.nextLine();
            if (two.matches("2")) {
                double x = 0;
                int three = reply.nextInt();
                while (three != 3) {
                    try {
                        TimeUnit.MILLISECONDS.sleep(1);
                        x = x+.001;
                        System.out.println(x);
                        three = reply.nextInt();
                    } catch (InterruptedException e) {
                }
               }
          }
0

There are 0 best solutions below