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

• October 3rd, 2012, 08:53 AM
looper
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.
• October 3rd, 2012, 09:09 AM
curmudgeon
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.
• October 3rd, 2012, 09:14 AM
KevinWorkman
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.
• October 3rd, 2012, 09:22 AM
looper
Re: How do you write a timed 'while loop' which prints the no. of loops after a set t
Quote:

Originally Posted by curmudgeon
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:?
• October 3rd, 2012, 09:24 AM
looper
Re: How do you write a timed 'while loop' which prints the no. of loops after a set t
Quote:

Originally Posted by KevinWorkman
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...
• October 3rd, 2012, 09:52 AM
curmudgeon
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:

Code :

```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```
• October 3rd, 2012, 06:39 PM
jps
Re: How do you write a timed 'while loop' which prints the no. of loops after a set t
Quote:

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.