6 Replies Latest reply: Oct 20, 2012 11:08 AM by Thomas Morgan

# What I am doing wrong in this loop?

How I can change this code
to have the output like this

If the user Enter number of hours traveled = 3
and Please Enter The Speed of vehicle in MPH = 40
the method will calculate 1 * 40
2 * 40
3 * 40
I need to use For loop
Thank you, I am just learning JAVA

I need the output to look like this

/*
* Description Convert distance Traveled Parameters: Int: Speed, Number
* hours traveled (milePerhour,numberOfhoursTraveled,distance)
* Returns:Integer: Distance Traveled
*/
public static double convertDistanceTraveled(int milePerhour, int numberOfhoursTraveled) {
return (milePerhour * numberOfhoursTraveled);

// public static double convertDistanceTraveled(int c) {
// int dist = milePerhour * c;
// return dist;
}
}
``````Edited by: 893973 on Oct 18, 2012 1:37 PM

Edited by: 893973 on Oct 21, 2012 4:24 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ``````
• ###### 1. Re: What I am doing wrong in this loop?
>
How I can change this code
to have the output like this
>
Well you need to look at your output and see where the first line of output is wrong.
This is the first line of output
>
3          120
>
If that line is not correct ask yourself where those values came from.
The output is done by this line
``System.out.format ("%s\t\t%s\n",i,dist);``
So the 3 came from 'i' and the 120 came from 'dist'.

Is the 3 correct? If not then 'i' is not correct.

So look at your code one line at a time and try to figure out why the first value of 'i' is not correct.
``````        for (int i = numberOfhoursTraveled; i < milePerhour; i++) {

dist = (int) convertDistanceTraveled(milePerhour, numberOfhoursTraveled);``````
What is the value of 'i' the first time through the loop? It is numberOfhoursTraveled which is 3. Is that what you want?
• ###### 2. Re: What I am doing wrong in this loop?
Believe I look at new at the stop

3 is correct and the
milePerhour * numberOfhoursTraveled is also correct 3 x 40 = 120

Hour Distance Traveled
_________________________
1 40
2 80
3 120 .

System.out.println("Please Enter The Speed of vehicle in MPH \n");
40
System.out.println("Enter number of hours traveled")
3

so the problem is that if the user enter 3(hours traveled) the output should be
Hour Distance Traveled
_________________________
1 40
2 80
3 120 .

If they enter 2
the outout should be
2 80
3 120 .
etc..
• ###### 3. Re: What I am doing wrong in this loop?
>
3 is correct and the
milePerhour * numberOfhoursTraveled is also correct 3 x 40 = 120
>
You said you wanted the first line of output to be
``````Hour         Distance Traveled
_________________________
1                     40``````
But this is the first line of output that you are getting
``3 120``
So how can you say that the '3' above is correct. It should be '1'.

• ###### 4. Re: What I am doing wrong in this loop?
sorry, you right the out should be like this

the calculation is correct 3 x 40
(milePerhour * numberOfhoursTraveled);

the method is doing it right

what is wrong is that the out should be
1 40
2 80
3 120 .

isince the user entered 3 in the
("Enter number of hours traveled");

if they enter 2
the output should be
1 40
2 80

etc..
in another words the # of hours travled needs to be broken to calculate the miles x hour.. hor by hour
Thank you
• ###### 5. Re: What I am doing wrong in this loop?
>
in another words the # of hours travled needs to be broken to calculate the miles x hour.. hor by hour
>
Exactly. You can use 'i' in the for loop to do that. I gave you plenty of hints in my first reply.

Look at this typical 'for' loop in the Java tutorial and see the output it produces.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

Then modify your starting value of i like the loop and see what happens.
• ###### 6. Re: What I am doing wrong in this loop?
Hi,

Try changing your for loop as follows:

for (int i =1; i<=numberOfhoursTraveled; i++)
{
System.out.format("%s\t\t%s\n", i, convertDistanceTraveled(milePerHour, i));
}

Also change the return type for the convertDistanceTraveled function from double to int.

Hope this helps,

Thomas