# simple looping problem , problem with displaying

Page 1 of 1

## 7 Replies - 2192 Views - Last Post: 15 October 2012 - 12:47 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=295669&amp;s=25c03306f4cbbe6fa5dedc75bff3cd29&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 zerocool18

Reputation: 3
• Posts: 152
• Joined: 12-October 12

# simple looping problem , problem with displaying

Posted 14 October 2012 - 11:38 PM

been working on this problem that asks:

1) Ask the user for a speed in miles per hours.
2) Ask the user for the number of hours that the car has traveled.
3) The input should be validated so if the user inputs a negative value for either request. you should reprompt the user to give a valid answer.
4)Using the given information from the user display a table that shows the distance traveled for each hour.

Sample Run:
Enter the speed of the car in miles per hour: 40
Enter the total hours of travel time: 3

Hour Distance Traveled
1 40
2 80
3 120

now ive got most of it done ( pretty much all done) but i dont get how to display the distance traveled for EACH hour. right now , my program does everything , except it displays just the hour entered and the total distance. any tips for this?

```import java.util.Scanner;

public class Distance{
public static void main(String[] args) {

// creates the scanner
Scanner input = new Scanner(System.in);
// promt user for input

System.out.println("Enter the speed of the car in miles per hour: ");
int speed = input.nextInt();
System.out.println("Enter the total hours of travel time: ");
int hours = input.nextInt();

while ( speed <0 && hours <0){

System.out.println("Enter the speed of the car in miles per hour: ");
speed = input.nextInt();

System.out.println("Enter the total hours of travel time: ");
hours = input.nextInt();

}
System.out.println("Hours\tDistance Travaled(miles)");
System.out.println("---------------------------------");

System.out.println(hours+ "\t" + (speed * hours));

}

}

```
]

Is This A Good Question/Topic? 0

## Replies To: simple looping problem , problem with displaying

### #2 fromTheSprawl

• Monomania

Reputation: 515
• Posts: 2,063
• Joined: 28-December 10

## Re: simple looping problem , problem with displaying

Posted 14 October 2012 - 11:47 PM

Shouldn't it just be like this:
distance = total travelled hours - travelled hours from last hour

### #3 zerocool18

Reputation: 3
• Posts: 152
• Joined: 12-October 12

## Re: simple looping problem , problem with displaying

Posted 15 October 2012 - 12:07 AM

aaahhh!! so close! now i got it printing each hour , but the distance stays the same. now i gotta figure that out

```import java.util.Scanner;

public class Distance{
public static void main(String[] args) {

// creates the scanner
Scanner input = new Scanner(System.in);
// promt user for input

System.out.println("Enter the speed of the car in miles per hour: ");
double speed = input.nextDouble();
System.out.println("Enter the total hours of travel time: ");
double hours = input.nextDouble();
int i;

while ( speed <0 && hours <0){

System.out.println("Enter the speed of the car in miles per hour: ");
speed = input.nextInt();

System.out.println("Enter the total hours of travel time: ");
hours = input.nextInt();

}

System.out.println("Hours\tDistance Travaled(miles)");
System.out.println("---------------------------------");
for (i = 1; i <= hours; i++){

System.out.println(i+ "\t" + (speed * hours));
}

}

}

```

### #4 fromTheSprawl

• Monomania

Reputation: 515
• Posts: 2,063
• Joined: 28-December 10

## Re: simple looping problem , problem with displaying

Posted 15 October 2012 - 12:20 AM

It seems to me your program is designed so that for each hour there is a static distance. Then wouldn't it be easier to do distance divided by hour?

### #5 zerocool18

Reputation: 3
• Posts: 152
• Joined: 12-October 12

## Re: simple looping problem , problem with displaying

Posted 15 October 2012 - 12:31 AM

fromTheSprawl, on 15 October 2012 - 12:20 AM, said:

It seems to me your program is designed so that for each hour there is a static distance. Then wouldn't it be easier to do distance divided by hour?

i tried that , but it displays it backwards. it prints

1. 120
2. 60
3. 40

```System.out.println(i+ "\t" +((speed*hours)/i));

```

### #6 fromTheSprawl

• Monomania

Reputation: 515
• Posts: 2,063
• Joined: 28-December 10

## Re: simple looping problem , problem with displaying

Posted 15 October 2012 - 12:40 AM

Wouldn't it be just iterating through the total hours and printing the input distance since that's the result anyway?

Oh, I see. Just multiply the current value of i to the initial speed and you'll get what you want.

### #7 zerocool18

Reputation: 3
• Posts: 152
• Joined: 12-October 12

## Re: simple looping problem , problem with displaying

Posted 15 October 2012 - 12:42 AM

woo , i got it working lol. thanks for the suggestions. just to substitute i into my distance formula.

fromTheSprawl, on 15 October 2012 - 12:40 AM, said:

Wouldn't it be just iterating through the total hours and printing the input distance since that's the result anyway?

Oh, I see. Just multiply the current value of i to the initial speed and you'll get what you want.

haha yeah , you replyed just as i found that out. thanks for reply anyhow! thats exactly what i had to do

### #8 fromTheSprawl

• Monomania

Reputation: 515
• Posts: 2,063
• Joined: 28-December 10

## Re: simple looping problem , problem with displaying

Posted 15 October 2012 - 12:47 AM

Awesome! Congratulations.