Hey guys - I need some help...
I'm trying to create a program 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 and another to DisplayData, 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.
Here's the Errors I'm getting from the compiler:
DistTravel.java:25: error: variable speed might not have been initialized
displayData(speed, time);
^
DistTravel.java:25: error: variable time might not have been initialized
displayData(speed, time);
^
2 errors
P.S. Bare with me on my coding. I am an amateur at Java Programming. Still learning.
Code:import javax.swing.JOptionPane; import java.io.*; public class DistTravel { public static void main(String[] args) throws IOException { // Declare the variables. String input; int speed, time; // The 'Distance Traveled' equation. Distance(); // Display Data displayData(speed, time); System.exit(0); } public static int 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) { for(int i = 1; i <= time; i++) { JOptionPane.showMessageDialog(null, "Hour(s): Distance Traveled" + "----------------------------" + "Hour" + ": " + i + (speed * i) + " miles traveled"); } } }


Reply With Quote

Bookmarks