Calculate Distance Traveled (using Methods)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: Calculate Distance Traveled (using Methods)

  1. #1
    Join Date
    Nov 2011

    Calculate Distance Traveled (using Methods)

    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: error: variable speed might not have been initialized
    displayData(speed, time);
    ^ 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.

    import javax.swing.JOptionPane;
    public class DistTravel
    	public static void main(String[] args) throws IOException
    		// Declare the variables.
    		String input;
    		int speed, time;
    		// The 'Distance Traveled' equation.
    		// Display Data
    		displayData(speed, time);
    	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");

  2. #2
    Join Date
    May 2006

    Re: Calculate Distance Traveled (using Methods)

    The error messages tell you what is wrong ie:

    variable speed might not have been initialized: means the variable called 'speed' is being used before you have definitely assigned a value to it - remember class and instance variable are given default values when they are declared but local variables are not.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a survey!

HTML5 Development Center