# Calculate Distance Traveled

Page 1 of 1

## 11 Replies - 4326 Views - Last Post: 14 November 2011 - 02:24 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=255208&amp;s=26fb42a145915ffb3ace9387efb6ed1f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 6r3nt0n

Reputation: 0
• Posts: 10
• Joined: 10-November 11

# Calculate Distance Traveled

Posted 10 November 2011 - 08:20 PM

Hey guys - I need some help...

I'm trying to create a problem that calculates Distance Traveled using user-input for their Speed and Time. However, the assignment requires that I write a separate method for the computation of the Distance, so I can recall it back in the Main method, then display the results in another method. But - I keep getting some errors. Any help will surely be appreciated. Thanks in advance.

P.S. Bare with me on my coding. I am an amateur at Java Programming. Still learning.

```import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;

public class distTravel
{
public static void main(String[] args)
{
// Variables
int dist, speed, time;

// The 'Distance Traveled' equation.
distance(speed, time);

// Display Data
for(int i=1; i<=time; i++)
displayData(i, speed);

System.exit(0);
}

public static double distance(int speed, int time)
{
// Get the vehicle speed from the user.

String input;	// To hold user's input.

input = JOptionPane.showInputDialog("What is your vehicle's speed in miles-per-hours? ");

// Convert the input to a double.
speed = Integer.parseInteger(input);

while (speed <= 0)
{
input = JOptionPane.showInputDialog("Your speed must be greater than zero. Please re-enter. ");

speed = Integer.parseInteger(input);
}

// Get the hours traveled from the user.
input = JOptionPane.showInputDialog("How many hours have you traveled, thus far? ");

time = Integer.parseInteger(input);

while (time <= 1)
{
input = JOptionPane.showInputDialog("How many hours have you traveled for? ");

time = Integer.parseInteger(input);
}

return speed * time;
}

public static void displayData(int i, int speed)
{
JOptionPane.showMessageDialog(null, "Hour(s): Distance Traveled" +
"----------------------------" + "Hour " + i + ": " + (speed * i) + " miles traveled");
}
}

```

This post has been edited by macosxnerd101: 10 November 2011 - 08:21 PM
Reason for edit:: Please use a descriptive title

Is This A Good Question/Topic? 0

## Replies To: Calculate Distance Traveled

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11315
• Posts: 42,637
• Joined: 27-December 08

## Re: Calculate Distance Traveled

Posted 10 November 2011 - 08:21 PM

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Calculate Distance Traveled

Posted 10 November 2011 - 08:21 PM

And the question is ?

### #4 6r3nt0n

Reputation: 0
• Posts: 10
• Joined: 10-November 11

## Re: Calculate Distance Traveled

Posted 10 November 2011 - 09:02 PM

My apologies. I forgot to post the errors.

distTravel.java:40: error: cannot find symbol
speed = Integer.parseInteger(input);
^
symbol: method parseInteger(String)
location: class Integer
distTravel.java:46: error: cannot find symbol
speed = Integer.parseInteger(input);
^
symbol: method parseInteger(String)
location: class Integer
distTravel.java:52: error: cannot find symbol
time = Integer.parseInteger(input);
^
symbol: method parseInteger(String)
location: class Integer
distTravel.java:58: error: cannot find symbol
time = Integer.parseInteger(input);
^
symbol: method parseInteger(String)
location: class Integer
4 errors

### #5 x68zeppelin80x

Reputation: 130
• Posts: 576
• Joined: 07-March 09

## Re: Calculate Distance Traveled

Posted 10 November 2011 - 09:09 PM

Why does distance have parameters?

Use Integer.parseInt() NOT Integer.parseInteger()

This post has been edited by x68zeppelin80x: 10 November 2011 - 09:21 PM

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Calculate Distance Traveled

Posted 10 November 2011 - 09:20 PM

The only static method should be main()
This method should create a distTravel object
and then play with it

More you have staic methods in your class, more bad is your design and more you are away from OO programming

variables like

int dist, speed, time;

should be instance variable of the distTravel class NOT part of the main() method

### #7 x68zeppelin80x

Reputation: 130
• Posts: 576
• Joined: 07-March 09

## Re: Calculate Distance Traveled

Posted 10 November 2011 - 09:47 PM

Try to write a toString() that appends each mile and returns all of the data. Then call the toString() inside the showMessageDialog().
I say this because the code you have makes a popup for every mile traveled and if the user enters 42 miles, they will have to close ALL 42 popup boxes...

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Calculate Distance Traveled

Posted 10 November 2011 - 10:29 PM

x68zeppelin80x, on 11 November 2011 - 12:47 AM, said:

Try to write a toString() that appends each mile and returns all of the data. Then call the toString() inside the showMessageDialog().
I say this because the code you have makes a popup for every mile traveled and if the user enters 42 miles, they will have to close ALL 42 popup boxes...

Won't work as there is no distTravel object !!!

We have to show this newbie how to get ride of these static methods

main() should just instansiate a distTravel object and then play with it

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Calculate Distance Traveled

Posted 10 November 2011 - 10:49 PM

### #10 6r3nt0n

Reputation: 0
• Posts: 10
• Joined: 10-November 11

## Re: Calculate Distance Traveled

Posted 13 November 2011 - 02:32 PM

Alright - I fixed a few of the errors you guys helped me out with. However, now I'm getting these errors from the compiler. Any ideas? I'm stuck. Thanks again for the help.

Errors:
DistTravel.java:70: error: cannot find symbol
"----------------------------" + "Hour " + i + ": " + (speed * i) + " miles traveled");
^
symbol: variable i
location: class DistTravel
DistTravel.java:70: error: cannot find symbol
"----------------------------" + "Hour " + i + ": " + (speed * i) + " miles traveled");
^
symbol: variable i
location: class DistTravel
2 errors

```import javax.swing.JOptionPane;

public class DistTravel
{
public static void main(String[] args)
{
// Declare the variables.
int time;
int speed;

// The 'Distance Traveled' equation.
distance();

// Display Data
for(int i=1; i<=time; i++)
displayData(speed, time);

System.exit(0);
}

public static double distance()
{
// Get the vehicle speed from the user.

// Declare the variables
int speed;
int time;

String input;	// To hold user's input.

input = JOptionPane.showInputDialog("What is your vehicle's speed in miles-per-hours? ");

// Convert the input to a double.
speed = Integer.parseInt(input);

while (speed <= 0)
{
input = JOptionPane.showInputDialog("Your speed must be greater than zero. Please re-enter. ");

speed = Integer.parseInt(input);
}

// Get the hours traveled from the user.
input = JOptionPane.showInputDialog("How many hours have you traveled, thus far? ");

time = Integer.parseInt(input);

while (time <= 1)
{
input = JOptionPane.showInputDialog("How many hours have you traveled for? ");

time = Integer.parseInt(input);
}

return speed * time;
}

public static void displayData(int speed, int time)
{
JOptionPane.showMessageDialog(null, "Hour(s): Distance Traveled" +
"----------------------------" + "Hour " + i + ": " + (speed * i) + " miles traveled");
}
}

```

### #11 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Calculate Distance Traveled

Posted 13 November 2011 - 09:05 PM

Nobody is short sighted here, we really do not need your bold tags
Better to put your energy in learning Java rather tan fancy forum editing

"----------------------------" + "Hour " + i + ": " + (speed * i) + " miles traveled");

What is the variable "i" referenced twice on that line ?

### #12 6r3nt0n

Reputation: 0
• Posts: 10
• Joined: 10-November 11

## Re: Calculate Distance Traveled

Posted 14 November 2011 - 02:24 AM

Sorry about the bold. Just trying to separate my post from the Error list.

The variable "i" is referenced twice because I was wanting to show each Hour as it increases.
Similar to the picture below. But I'm getting a few Errors on the displayData() method.