1

A program that prompts the user to enter the number of seconds, then displays a message at every second, and terminates when the time expires. So far, I have been able to do this. But, I am stuck here.

public static void main(String[] args) 
    {
    Scanner r = new Scanner(System.in); 
        int sec = r.nextInt(); 
        while (sec > 0)
        { 
            System.out.println("Seconds Remaining" + sec); 
            /**What to do here using System.currentTimeMillis()??**/ 
            sec--;
        }
}
Katherine
  • 281
  • 1
  • 2
  • 13
  • I am pretty sure they haven't been covered so far. Is there any other way?? @Reimeus – Katherine Aug 24 '15 at 19:17
  • 1
    http://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html The example in the documentation is almost identical to yours... – Brick Aug 24 '15 at 19:19
  • 1
    You can also do spin loop and check difference between start time and current time. Not that i would recommend it, but if you didn't learn about the threads.. – John Aug 24 '15 at 19:25

3 Answers3

3

You are looking for Thread.sleep() or TimeUnit.sleep I suspect

public static void main(String[] args) throws InterruptedException {
    Scanner in = new Scanner(System.in); 
    for(int sec = in.nextInt(); sec > 0; sec --) {
        System.out.println("Seconds Remaining " + sec); 
        TimeUnit.SECONDS.sleep(1);
    }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

From a "how can I make this work within the selected methodology (using currentTimeMillis) which has some value for the sake of learning so that the next time I need to do something similar and it's the right way I won't be stuck, even though it's not the best way this time" perspective:

long start = System.currentTimeMillis();
long now = System.currentTimeMillis();
while (now - start > 1000) // While the difference between the two times is less than a second
{
    now = System.currentTimeMillis();
}

You could even try to correct for the error (now-start-1000 could be greater than 1 after all, that would be lost time) and calculate any excess time. You would then take that excess and subtract it from the 1000 in the loop's conditional so that the next time around you'd wait just a little less time to make up for the excess from the last time. Also, the System.out.println() takes time, therefore you'd want to set start before the System.out.println in order to be a little bit more accurate.

Now, hopefully I've pointed out enough of the pitfalls to demonstrate why this is a bad idea for any important timekeeping. The simplest way to be more accurate involves the use of Timer which uses threads allowing for the printing and other overhead to be separated out from the timekeeping. However, it simply uses a less interesting but more simple explanation for the above which is Object.wait() which "does not offer real-time guarantees".

John C
  • 500
  • 3
  • 8
3

Using thread is the most appropriate way.

public static void main(String[] args) throws Exception 
{
    Scanner r = new Scanner(System.in); 
    int sec = r.nextInt(); 
    while (sec > 0)
    { 
        System.out.println("Seconds Remaining" + sec); 
        /**What to do here using System.currentTimeMillis()??**/ 
        Thread.sleep(1000);
        sec--;
    }
}

Hope this helps :)

bane19
  • 384
  • 2
  • 15