Domanda

I'm trying to make a simple alarm clock as my first java project. However, I cannot get my code to continously read the time. I am just using the simpleclock thing in java and I'm only going to worry about the hour and minute for now.

I want to find out how to update my "int hour" and "int minute." I'm thinking of reading the variables inside my infinite while loop.

Any help is appreciated, thanks!

Calendar now= Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute=now.get(Calendar.MINUTE);

int k=1;
while (k==1) {

    System.out.println(hour);
    System.out.println(minute);


}
È stato utile?

Soluzione

You should use a thread for this instead of constantly printing out the time. See the post above if that's what you (most likely) need; however, for purposes of learning what you did wrong, here's an explanation.

You are not updating your hour and minute variables as time goes on.

For example, if you ran this program at 2:52, before you enter the while loop, hours would initially be 2, and minutes would initially be 52. Then, once you enter the while loop, hours=2, and minutes=52, and will never change.

In other words, once you enter the scope of the while loop, as indicated by the { and }, you will not run the previous three statements above the while loop ever again.

Also, since the while loop will evaluate a condition, you can just replace your k==1 trick with true, which is a condition which will always evaluate to true. Both ways are equivalent, but the latter is more elegant.

Here is a solution:

while (true) {

Calendar now= Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute=now.get(Calendar.MINUTE);

System.out.println(hour);
System.out.println(minute);

}

Altri suggerimenti

move the calls to new.get inside the loop.

You should not while loop continuously to get the time. Instead, use Thread.sleep(int millis) to make your code wait for each minute. This puts less strain on the processor (your implementation keeps the processor busy with useless updates constantly). Also, to get the time, you should move calls to now.get(...) inside the loop. Try this:

Calendar now= Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute=now.get(Calendar.MINUTE);
while (true) {
    hour = now.get(Calendar.HOUR_OF_DAY);
    minute=now.get(Calendar.MINUTE);
    System.out.println(hour);
    System.out.println(minute);
    // sleep for 5 secs (so minute updates will be accurate to 5 secs)
    // Thread.sleep is not always precise and inaccuracies
    // could build up if we slept for 1 minute
    try { Thread.sleep(5000); } catch(Exception e){}
    // later on when you build more complex programs,
    // you will make use of the catch block, but for now, ignore it
}

You'd need to move the now.get methods to the while loop.

If you don't want to keep doing unnecessary updates you could make a timer and only update once every couple of seconds:

import java.util.Timer; // import Timer

// Some Codes Here

Timer timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        // Whatever you want to do with the time, lets say print
        Calendar now= Calendar.getInstance();
        int hour = now.get(Calendar.HOUR_OF_DAY);
        int minute=now.get(Calendar.MINUTE);
        System.out.println(hour);
        System.out.println(minute);
    }
}, 4*1000, 4*1000);     // time to trigger in milliseconds and time to repeat
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top