-1

I'm trying to create my own timer with this and want to make a punch clock using this timer but my loop isn't repeating (Time: 0:0) it only repeats after I enter in 2 numbers and I can't figure out why please help I used to have Thread.sleep(60000) below minute = minute + 1 so I have it working on a minute rate but removed so I could see results faster

import java.util.Scanner;

public class Main {



        public static void main(String[] args) throws InterruptedException {

            Scanner sc = new Scanner(System.in);
            int minute = 0, hour = 0, punchi = 0, puncho = 0, TP, TH = 0, i = 0, o = 0;
            int power = 10;
            boolean k = true;
            String si, so;


        while (k == true) {

            System.out.println("Time: " + hour + ":" + minute);
            k = true;

            minute = minute + 30;

            k = true;
            if (minute == 60) {
            hour = hour + 1;
            minute = 0;
             k = true;
            }

            else
                k = true;

            if (hour == 24) {
                hour = 0;
                k = true;
            }
            else
                k = true;

            si = sc.nextLine();
            so = sc.nextLine();

                    if (si == "punchi") {
                        i = sc.nextInt();}
                    else
                        k = true;
                        i = sc.nextInt();

                    if (so == "puncho"){
                        o = sc.nextInt();

                    }
                    else
                        k = true;

                    if (o > 0) {
                        TH = (TH + o - i);
                        o = 0;
                        i = 0;

                        System.out.println("Power off?");
                            power = sc.nextInt();
                            k = true;}
                    else
                        k = true;

                        if (power == 9) {
                            TP = TH * 14;
                            k = false;
                            System.out.println("your total pay is " + TP); }

                        else 
                            k = true;

                    }






            }

        }

}
Hearen
  • 7,420
  • 4
  • 53
  • 63
Josh
  • 1
  • 1

1 Answers1

0

The reason it doesn't loop until you enter two numbers is because the scanner's next line function you are using blocks until it receives a value, since you have two next line calls, it requires two values to continue the loop.

If it didn't block, the loop could potentially happen so fast that you couldn't type in a sentence because by the time that iteration of the loop runs you may have only typed one letter.

If you wish to use this function, you need to multithread your program so the while loop runs on the main thread, the next line function runs a second thread, and when the second receives what it's looking for, it informs the first thread.

MarcusOuelletus
  • 150
  • 2
  • 8
  • So I would have the first while loop out putting the timer and put a thread.sleep after the minute = minute + 1 then another loop looking for output to equal punchi and puncho then give them the values of the timer when they are outputted then minus punchi from puncho then have the end loop for the first loop looking for “off” then when off pops up it closes the loop calculating total pay – Josh Aug 16 '18 at 11:23
  • If you always expect a punch in before any punch out, you can put the two read lines in the same thread, the punch in being called first. If the punches in and out aren't in order they'll need to be in separate threads so they don't block each other. – MarcusOuelletus Aug 16 '18 at 13:46