Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Making a countdown timer using the currentTimeMillis method

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Making a countdown timer using the currentTimeMillis method

    Hey guys,

    Recently I made the decision to learn Java programming on my own (I only bought a book, no course). So far I've covered the following relevant topics:
    -Elementary Programming: Identifiers, Variables, Data types...
    -Selections: boolean, if (else if, else) statements, logical operators, switch statements...
    -Loops: while, do-while, for, nested loops, keywords break and continue... (=> Currently doing the exercises of this chapter).

    In one of the last exercises of the latter chapter I'm asked to make a countdown timer. The only method to capture time the book has covered yet is the currentTimeMillis method in the System class.
    I'm aware this problem probably can be solved in an easier and more efficient way using the Timer class, but imagine I follow the book strictly and I have never heard of it.

    So far I have tried a number of things, without succes obviously, this is the best I came up with:

    import java.util.Scanner;
    	public class Exercise4_43 {
    		public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    		System.out.print("Enter the number of seconds: ");
    		int interval = input.nextInt();
    		long startTime = System.currentTimeMillis();
    		long endTime = System.currentTimeMillis() + (interval * 1000);
     
    		while (System.currentTimeMillis() < endTime) {
    			if ((endTime - System.currentTimeMillis()) % 1000 == 0){
    				System.out.println(((endTime - System.currentTimeMillis()) / 1000) + " seconds remaining.");
    				}
    			}
    		System.out.println("Stopped.");
    	}
    }

    When I run the program and e.g. enter 10 as input I get the following output:

    10 seconds remaining
    //1 second later
    9 seconds remaining
    9 seconds remaining
    9 seconds remaining
    9 seconds remaining
    //another second later
    8 seconds remaining
    8 seconds remaining
    8 seconds remaining
    8 seconds remaining
    //another second later
    7 seconds remaining
    7 seconds remaining
    7 seconds remaining
    7 seconds remaining

    ....

    0 seconds remaining
    Stopped.

    Can someone please give me a hint so I can solve this problem? (Please, don't give a possible solution, if I can't solve the problem myself I miss the opportunity to acquire some extra insight in this kind of problems).
    Thanks in advance!

    Cheers
    Michael


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a countdown timer using the currentTimeMillis method

    Can you explain what is the problem you are having?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Making a countdown timer using the currentTimeMillis method

    Excuse me for not being clear enough explaining the problem.
    The timer works fine, but why does it give every second 3-4 times the same output instead of just showing it once every second?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a countdown timer using the currentTimeMillis method

    why does it give every second 3-4 times the same output
    Try debugging the code by adding some more println statements that print out the values of the variables that control when the code prints out a message. The print out will show you what the computer sees when it executes the code and help you understand why it does what it does.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Michael_D (April 30th, 2013)

  6. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Making a countdown timer using the currentTimeMillis method

    Thank you for your respons! Although I couldn't figure out why the program kept repeating the same output, I found another possible solution.
    I got lost of the remainder operator and tried something else.

    import java.util.Scanner;
    public class Exercise4_43{
    	public static void main(String[] args){
    		Scanner input = new Scanner(System.in);
     
    		System.out.print("Enter the number of seconds: ");
    		int interval = input.nextInt();
     
    		long startTime = System.currentTimeMillis() / 1000;
    		long endTime = startTime + interval;
     
    		while (System.currentTimeMillis() / 1000 < endTime) {
    			while (startTime != System.currentTimeMillis() / 1000) {
    				startTime += 1;
    				if (endTime - startTime > 1)
    					System.out.println(endTime - startTime + " seconds remaining.");
    				else if (endTime - startTime == 1) {
    					System.out.println("1 second remaining.");
    					}
    				}
    			}
    		System.out.println("Time up.");
    		}
    	}
    Now the programs works properly. Only too bad of the precison loss of the first second... //(long / 1000)

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a countdown timer using the currentTimeMillis method

    why the program kept repeating the same output
    DId you print out the value of System.currentTimeMillis() every time a the "remaining" message was printed?
    Did the value that was printed change when the same seconds were printed?
    How fast is your PC? Can it execute the code and loop several times in less than a millisec?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Making a countdown timer using the currentTimeMillis method

    Yes. Every time I wanted to print the remaining value I used: (endTime - System.currentTimeMillis()) / 1000).
    Once printed, 3-4 identical lines with the remaining seconds t appeared. One second later, another 3-4 identical lines with the t-1 remaining seconds appeared.
    My processor is an Intel Core i3 CPU 530 @ 2.93 GHz, so far from high-end. I doubt this would be the cause of the problem because tons of PC's are faster than mine.
    Or maybe no one has tried to put a remainder in their algorithm?

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making a countdown timer using the currentTimeMillis method

    2.9 GHz is fast when talking about millisecs. A loop can go around many times in a millisec.
    If you printed System.currentTimeMillis() with the remaining msg the same value would be printed many times before the clock moved to the next millisec.
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    Michael_D (April 30th, 2013)

  11. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Making a countdown timer using the currentTimeMillis method

    Thanks man, I didn't know that.
    Lucky for me I tried something else, otherwise I would still be fooling around with the remainder operator.

  12. #10
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a countdown timer using the currentTimeMillis method

    Dear Michael, I don't know if you already found the answer,so sry if i repeat something already written, but i was programming the same exercise myself and found your method really useful! Just added a few lines to complete! Here it is!
    import java.util.Scanner;

    public class Exercise_4_43 {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the number of seconds:");
    int seconds = input.nextInt();
    long startTime = System.currentTimeMillis();
    long endTime = System.currentTimeMillis() + (seconds * 1000);
    long temp = startTime + 1000 ;
    System.out.print(seconds + " seconds remaining.\n" );
    while (startTime < endTime) {
    if (startTime == temp) {
    seconds--;
    System.out.print(seconds + " seconds remaining.\n");
    temp += 1000;
    }
    startTime = System.currentTimeMillis();
    }
    System.out.println("Stopped.");
    }
    }

  13. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Making a countdown timer using the currentTimeMillis method

    @Damien Blackbox, welcome to the forums. Typically threads over a few weeks old can be considered ancient in forum time, so we try to encourage members to avoid resurrecting them. Should you find other (hopefully newer) threads to contribute to, I would also recommend reading the forum guidelines and http://www.javaprogrammingforums.com...n-feeding.html

Similar Threads

  1. Countdown timer
    By AANZ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2013, 07:46 AM
  2. Please help me with my Java Countdown Timer
    By remagarrote in forum AWT / Java Swing
    Replies: 3
    Last Post: March 8th, 2013, 10:09 AM
  3. Timer Countdown - Code Syntax
    By baddack in forum What's Wrong With My Code?
    Replies: 22
    Last Post: September 26th, 2012, 03:56 PM
  4. Problems with JNI and currentTimeMillis function
    By steph311 in forum Java Native Interface
    Replies: 2
    Last Post: July 9th, 2012, 10:01 AM
  5. Timer class countdown
    By p0oint in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2010, 03:31 AM