0

Is it possible to make a timer which counts down in seconds in a Java console application? So is it possible to say count 10 seconds; print a statement; count another 10 seconds and print another statement etc. If so how could I do it?

James
  • 338
  • 1
  • 2
  • 16

1 Answers1

0
    // This is how to print with a delay
    // Thread.sleep stops the program executoin for an amount of time
    System.out.println("String 1");


    try {
        Thread.sleep(1000);  // Thread.sleep takes in milliseconds
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println("String 2");

    // This is how to count down with a delay. Its accuracy is questionable, because
    // printing and increminting k take time, but it should be very close to a second.
    for(int k = 10; k > 0; k--)
    {
        System.out.println(k);

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
arathnim
  • 16
  • 1
  • It stops the thread, in this case, the program for a number of milliseconds. take a look at the java documentation for [Thread.Sleep](http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep(long)) – arathnim May 17 '14 at 15:19