Create a Countdown in Java

Generally, a backward counting starting from any number to zero in a fixed interval of time(in seconds) is called a countdown. It is mostly used in exams and all to show the time remaining.

Steps to follow:

  • Use sleep() and currentThread() of Thread class to pause the output of thread at the console. Thread class is the predefined class and sleep() and currentThread() are static methods. sleep() takes a parameter in ms (mili sec). currentThread() is used to target the thread which is currently being executed.
  • Since the above throws an exception, therefore try to write it in the try-catch block.
  • Use “\033[H\033[2J” for escaping and use the predefined method flush() to clear the console. “\033[H\033[2J” is called an ANSI code in which the first four characters i.e. \033 means escape character and combined with [H we are able to move the cursor to any position, if not mentioned then to the upper left corner(starting position) of the cmd/console. \033[2J is used to clean the cmd/console. flush() is also a pre-defined static method of System class to clear the remaining bytes if present by using other methods of System class like System.out.println(). flush() may or may not be used according to our requirements.
  • Use any loop to iterate the process from any number to zero and use an if statement to break out through the loop by clearing the console/output screen showing a message “time over” at last.
  • Note: Other classes and methods are also present to create a countdown in Java, but this is the most preferable.

Java countdown program

public class Main
{
  public static void main(String[] args) {
  System.out.println("count down begins: ");      // this message will show the message that countdown has begun
  try {
     Thread.currentThread().sleep(1000);        //this will stop the execution for 1000 ms(=1 s) and the next output will be shown after 1 s. It also threxceptionstionwrittentena  in try block
      } catch (Exception e) {                     //it will catch the exception if any
     System.out.println(e);
      }
  for(int i=10;i>=0;i--)                          //it will iterate from any number to zero. we have taken any number=10
  {
       System.out.print("\033[H\033[2J");          //it is used for escaping
        System.out.flush();                         //it will clear the console
      System.out.println("time left : "+i);       //it will show the time left 
      try {
     Thread.currentThread().sleep(1000);
      } catch (Exception e) {
     System.out.println(e);
      }
      if(i==0)                                    //if the iteration reaches zero, it will break out from the loop by clearing the console
       {
          System.out.print("\033[H\033[2J");
            System.out.flush();
          break;
      }
  }
  System.out.println("time over... ");            //this message will show the countdown is completed
  }
}

Note:

Run the above code for output.

Leave a Reply

Your email address will not be published. Required fields are marked *