0

Attempting to make a clock in java in my spare time, having lots of fun and two hours in i am still getting problems with my code.

it tells me the current time but advances way too quickly.

(copy and paste it into an IDE/Compiler and run it and you will see)

Can anyone help a beginner?

package clock;

import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

public class Clock {


    public static void main(String[] args) {

        int hour;
        int minute;
        int second;
        Scanner keyb = new Scanner(System.in);


        System.out.println("What hour is it?");
        hour = keyb.nextInt();
        System.out.println("What Minute is it?");
        minute = keyb.nextInt();
        second = 0;
        Timer t = new Timer();
        t.schedule(new TimerTask() {
            Scanner keyb = new Scanner(System.in);
            int hour;
            int minute;
            int second;
            int turnOn;
            @Override
            public void run() {
                turnOn = 1;
                while (turnOn != 0){
                    System.out.println("Current time is:"+ hour +":"+minute+":"+second);
                    second ++;
                    if (second >= 59){
                        minute ++;
                        second = 0;
                    }
                    else{

                    }
                    if (minute >= 59){
                        hour ++;
                        minute = 0;
                    }
                    else{

                    }
                    if (hour >= 24){
                        hour = 0;
                    }

                }
            }

        }, 0, 1000);
    }

}
xenteros
  • 15,586
  • 12
  • 56
  • 91

3 Answers3

1

You have scheduled your infinite loop to be started every second. t.schedule(TimerTask, long, long) starts at 0 properly, but then runs your run method every second. It would be correct if you incremented second in each call of the method run. Unfortunately instead of incrementing once, you start infinite loop which increments in every execution.

    Timer t = new Timer();
    t.schedule(new TimerTask() {
        Scanner keyb = new Scanner(System.in);
        int hour;
        int minute;
        int second;
        boolean turnOn = true;
        @Override
        public void run() {
//CHANGE HERE!!! \/
//You only want to increment seconds once in a call of run method.
            if (turnOn){
                System.out.println("Current time is:"+ hour +":"+minute+":"+second);
                second ++;
                if (second >= 59){
                    minute ++;
                    second = 0;
                }
                if (minute >= 59){
                    hour ++;
                    minute = 0;
                }
                if (hour >= 24){
                    hour = 0;
                }
            }
        }

    }, 0, 1000);

Is what you're looking for.

As a side remark please have in mind, that empty else blocks are redundant as well as empty lines. You don't need them in your code. What's more it's inappropriate to post them on StackOverflow. Your example should be minimal.

xenteros
  • 15,586
  • 12
  • 56
  • 91
0

There are various things to consider: (1) wait for 1 second after every loop. (ignoring processing time in loop) (2) put minute and hours logic in 'else if' block. Something like this

import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

public class Clock {

    public static void main(String[] args) {

        int hour;
        int minute;
        int second;
        Scanner keyb = new Scanner(System.in);

        System.out.println("What hour is it?");
        hour = keyb.nextInt();
        System.out.println("What Minute is it?");
        minute = keyb.nextInt();
        second = 0;
        Timer t = new Timer();
        t.schedule(new TimerTask() {
            Scanner keyb = new Scanner(System.in);
            int hour;
            int minute;
            int second;
            int turnOn;

            @Override
            public void run() {
                turnOn = 1;
                while (turnOn != 0) {
                    System.out.println("Current time is:" + hour + ":" + minute
                            + ":" + second);
                    second++;
                    if (second >= 59) {
                        minute++;
                        second = 0;
                        if (minute >= 59) {
                            hour++;
                            minute = 0;
                            if (hour >= 24) {
                                hour = 0;
                            }
                        }
                    }
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }, 0, 1000);
    }
}
ManishKr
  • 211
  • 2
  • 9
-1

I have fixed this by changing the turnOn int to 0 after the while loop which will stop the while loop continuing until the next second when it resets it to 1