Hi all. I'm having trouble with this simple Java program. When I compile it, numerous errors would happen. I put in let's say 50 miles for the speed of the car and 2 hours for the traveling hours. I run it and it you see this:

Hours Distance Traveled
---------------------------------
2 100
2 100

It doesn't display the 1 hour which it's supposed to be 50 miles traveled.

Also, I try to test the while loops with negative or 0 inputs, and I get a repeat of the output which looks like this:

Enter the speed of the vehicle in miles-per-hour: -1
Enter the number of hours that you have traveled: 0
Hours Distance Traveled
---------------------------------
2 2
2 2

I have been stuck finding the solution for a while, and I'd like some help, please. Thank you. Here's my code:

``````// This program asks the user for the speed of a vehicle (in miles-per-hour) and
// the number of hours it has traveled.
// Chapter 4 #2 Programming Challenge.
// Programmed by X.
// 2/27/2012.

import java.util.Scanner;

public class DistanceTraveled
{
public static void main(String args[])
{
String input;						// hold user's input
int MPH;								// miles per hour
int time;							// time in hours traveled
int i;								// placeholder for starting point of time
int distance;						// distance

// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// Get the number of miles driven from user.

System.out.print("Enter the speed of the vehicle in miles-per-hour: ");

MPH = keyboard.nextInt(); // converts the string into a numeric value.

while (MPH <= 0)
{

MPH++;
}

// Get the hours traveled from the user.

System.out.print("Enter the number of hours that you have traveled: ");

time = keyboard.nextInt(); // converts the string into a numeric value.

while (time <= 1)
{

System.out.print("Your hours must be at least 1 or more. Please re-enter.\n ");

time++;

}

// Formula for finding the distance.

distance = MPH*time;

// Output the distance that the vehicle has traveled for each hour.

System.out.println("Hours           Distance Traveled");
System.out.println("---------------------------------");

for (i = 1; i <= time; i++)
{

System.out.println(time + "\t\t" + (distance));
}
}
}``````

## All 4 Replies

Also, I try to test the while loops with negative or 0 inputs, and I get a repeat of the output which looks like this:

Why not ask for the input again instead of just incrementing the value?

It doesn't display the 1 hour which it's supposed to be 50 miles traveled.

The value of time and distance doesn't change use the counter i instead and you need to incorporate the formula in the loop to display the correct miles per every hour

you need to incorporate the formula in the loop to display the correct miles per every hour

I'm lost. which loop is this -- I assume the 'for loop'?

yes it's the for loop, the value of distance and time doesn't change
to fix this you need to make use of the counter as a substitute for time

I got it to work. I just had to put the i placeholder in the output in place of time, and substitute the i instead of distance for the distance formula. Man, it took me hours to fix this seemingly easy thing. Thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.