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

Thread: How do you write a timed 'while loop' which prints the no. of loops after a set time?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do you write a timed 'while loop' which prints the no. of loops after a set time?

    In Java, I need to write a while loop that will run for 10 seconds and after every 50 loops it will print the number of loops run so far in that set time.

    This is my attempt to the problem so far:

    public class WhileLoopProblem{
    public static void main(String[] args){
    WhileLoopProblem wlp = new WhileLoopProblem();
    wlp.timer();}

    public void timer(){
    int time = 0;
    int endtime= 10;
    long timeNow = System.currentTimeMillis();
    while(time < endtime)
    {System.out.println("Number of loops = "+time);t++; }}}


    When I print it just gives me the numbers 0 to 10...I know Im doing something wrong here but don't know how to correct it and make it print after every 50 loops run so far in that set time...


    Any help or advice will be greatly appreciated.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: How do you write a timed 'while loop' which prints the no. of loops after a set t

    You don't want to check the difference between your counter variables, but instead you want to check the seconds between your timeNow long and one obtained *inside of the while loop*. I'm guessing you're going to smack your head when you realize your logical error.

    Understand that the long returned by System.currentTimeMillis() is as the name suggests in milliseconds. You'll have to do very basic math to convert the difference into elapsed seconds.
    Last edited by curmudgeon; October 3rd, 2012 at 09:13 AM.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How do you write a timed 'while loop' which prints the no. of loops after a set t

    Should that t++ be a time++?

    Also, I don't know what more you expect this to do. You don't have any logic in there for comparing start time or current time- all you're doing is keeping track of the iterations, which is only one piece of what you need to do.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do you write a timed 'while loop' which prints the no. of loops after a set t

    Quote Originally Posted by curmudgeon View Post
    You don't want to check the difference between your counter variables, but instead you want to check the seconds between your timeNow long and one obtained *inside of the while loop*. I'm guessing you're going to smack your head when you realize your logical error.

    Understand that the long returned by System.currentTimeMillis() is as the name suggests in milliseconds. You'll have to do very basic math to convert the difference into elapsed seconds.
    Ok... I'm a complete noob at java here so I don't really understand what you mean by that... (even though I get the simple math part)

    :/

    What I want to do is create a while loop that times 5 seconds and after every 50 loops it will print the number of loops run so far in that set time. Are my int values uneccesary:?

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do you write a timed 'while loop' which prints the no. of loops after a set t

    Quote Originally Posted by KevinWorkman View Post
    Should that t++ be a time++?

    Also, I don't know what more you expect this to do. You don't have any logic in there for comparing start time or current time- all you're doing is keeping track of the iterations, which is only one piece of what you need to do.
    I changed it to System.out.println(+timeNow);timeNow++; and now I get java printing 1349274494256 forever...

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: How do you write a timed 'while loop' which prints the no. of loops after a set t

    The key to this question is the logic, not the Java. So in pseudocode, you want to do the following:

    get initial system time, put it in the initTime long variable
    create deltaTimeInSeconds variable and set to 0
    create int loop counter, loopCounter, and set to 0
    while loop until deltaTimeInSeconds is greater than or equal to 10
      increment loopCounter
      Check if loopCounter is divisable by 50 using mod operator, 
          if so, display it
      Get system time, 
      subtract from initTime, 
      divide this result by 1000 to convert millisec to seconds
      place result in deltaTimeInSeconds variable
    end while loop
    Last edited by curmudgeon; October 3rd, 2012 at 09:55 AM.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How do you write a timed 'while loop' which prints the no. of loops after a set t

    In Java, I need to write a while loop that will run for 10 seconds and after every 50 loops it will print the number of loops run so far in that set time.
    Break the solution down into steps.
    First make a java program.
    Then make a loop that runs for 10 seconds inside the program.
    Once this part works, add a counter to count the loops for the whole 10 seconds and print that out.
    After that works well, throw some logic in there to cause the print every 50 loops instead of just once at the end.

    Also please note:
    System.currentTimeMillis()
    does not start a timer at zero. Use your favorite search engine to determine what value the method returns.

Similar Threads

  1. how to implement timed out event in java?!
    By migongotar in forum Java Theory & Questions
    Replies: 3
    Last Post: December 15th, 2018, 07:41 AM
  2. when i run the program to find the y values it always prints y2 instead
    By jamesR in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 30th, 2012, 08:38 AM
  3. Loop only runs one time?
    By Purple01 in forum Loops & Control Statements
    Replies: 6
    Last Post: September 14th, 2012, 06:57 AM
  4. How to control the time that a function takes to execute in a loop?
    By GodspeedPR in forum Loops & Control Statements
    Replies: 4
    Last Post: July 20th, 2011, 03:37 PM
  5. nothing prints after runing code
    By darego in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2011, 11:14 AM