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 5 of 5

Thread: Java timer issue.

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java timer issue.

    Hi, I started learning java a bit less than a week ago, and a friend said that I should try writing a clock using java. I think that I have this clock working mostly, but when the loop in seconds reaches 59, it goes on to 60, and then passes a 1 into the minute integer, and sets the value of the second to 01. I know that this isn't really much of a big deal, but I would like to have the clock work properly, so it would reach 59 in seconds, pass a 1 to the minute, and continue at 00. Could someone have a look at my code and tell me what I have done wrong?
    By the way, because this is my first post, I had to deform the URL to post my code, sorry about that.
    pastebin com/nLBnPdYD
    Thanks.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java timer issue.

    Welcome to the forum! This is a fine looking early effort. (And deforming the URL is something most first-timers wouldn't even consider. They just complain they can't post links.)

    Using one of the <blank>bin services is okay for passing code among friends, but for getting help on most forums, it's preferred that the code is pasted directly into the topic using code tags. That way there's a history of the program's evolution whereas in a bin service one can only see the latest version which might make earlier questions and answers on a forum make no sense. You can learn more about using the Forum in a "new user FAQ" in a topic called "Announcement" at the top of each sub-forum.

    As for your question, try moving the statement "second +=1;" around some to see what difference the various locations have on the outcome. (Before or after the print statements is what really matters.)

    Oh, and class names should begin with capital letters, and don't give your classes the same name as existing core Java classes. Personalize them, like this one might be JPClock or MyTimer, etc.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java timer issue.

    Ok, thanks for the tips. Unfortunately, moving "second +=1" around didn't help, sometimes it would cut out the increment, and it would just resend the same number. It turns out that I didn't notice that the increment setting in the for loop was not being used, so I removed that. Anyway, here is my code again, this time internally.

    import java.util.Scanner;
    public class timer{
        public static void main(String[] args) {
        	Scanner input = new Scanner(System.in);
        	int hour, minute, second, day;
        	System.out.println("Please input the hour.");
        	hour = input.nextInt();
        	System.out.println("Please input the minute.");
        	minute = input.nextInt();
        	System.out.println("Please input the second.");
        	second = input.nextInt();
        	day = 0;
        	for(int i = 0; i< 9999;){
        	    try {
        	        Thread.sleep(1000);
        	    } catch(InterruptedException ie) {}
        	    second +=1;
        	    if (hour == 12){
            	    System.out.printf("%02d:%02d:%02d", hour, minute, second);
            	    System.out.print(" PM");
            	    System.out.println();
        	    }else if (hour > 12){
            	    System.out.printf("%02d:%02d:%02d", hour -12, minute, second);
            	    System.out.print(" PM");
            	    System.out.println();
        	    }
        	    else{
        	    System.out.printf("%02d:%02d:%02d", hour, minute, second);
        	    System.out.print(" AM");
        	    System.out.println();
        	    }
        		if (second >= 59){
        			minute += second / 60;
        					second %= 60;
        	    }
        		if (minute >= 59){
        			hour += minute / 60;
        				minute %= 60;
        		}
        		if (hour >= 24){
        			hour += day / 24;
        				hour %= 24;
        		}
     
        	}
        }
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java timer issue.

    Sorry. My first educated guess using my brain compiler and JVM is not always right.

    The key is in your first if statement that adjusts after second is incremented (reformatted):
    if (second >= 59)
    {
        minute += second / 60;
        second %= 60;
    }
    Talk yourself through what that code does starting with second = 59: The if statement is true, minute will be minute + 0 (doesn't change) and second will become (or remains) 59. But then before printing, second is incremented to 60, so that's what is printed. My thought was to move the increment until after the print statements to first print 59, but all that does is delay the inevitable, which is that 60 will be printed.

    Talk yourself through the above 'if' with second = 60 and see what happens.

    Try a slightly different approach: If second == 60, increment minute and set second = 0, AND ensure second isn't incremented until after the time has been printed. Maybe not elegant, but simple.

    This time I tried it on the silicon compiler, and it works for a small test range. You can test it more extensively.

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

    Default Re: Java timer issue.

    I wasn't really all that sure about when you meant there, but as I was looking at that if statement, I thought that I could make it a bit simpler, and remove the dividing, and the remainders, and turn the code into this.
        		if (second == 59){
        			minute += 1;
        					second = 0;
        	    }
    At that point, it was skipping the :60, but it was also skipping :00 therefore, I was losing a second. It occurred to me, that it was increasing just 1 too fast when it changed the 60 seconds into a minute, so I decided to try and counteract that. It may not be the most elegant way, but it is simple, and it works. Here is the if statement now.
        		if (second == 59){
        			minute += 1;
        					second = -1;
        	    }

    This if statement now works fine, and the timer works without any issues that I can see. Thank you for your help.

Similar Threads

  1. How to Use Timer in Java
    By neo_2010 in forum Java SE API Tutorials
    Replies: 2
    Last Post: August 6th, 2013, 09:49 AM
  2. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  3. Get Java input on a timer
    By johnsmith1992 in forum Threads
    Replies: 6
    Last Post: December 19th, 2011, 07:09 PM
  4. How to Use Timer in Java
    By neo_2010 in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: September 1st, 2009, 12:10 PM
  5. How to Use Timer in Java
    By neo_2010 in forum Java Programming Tutorials
    Replies: 0
    Last Post: September 1st, 2009, 12:10 PM