CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011

    need help debugging simple, beginner program

    I am supposed to answer the following question:

    "The Fast Freight Shipping Company charges the following rates:

    Weight of Package : Rate Per 500 Miles Shipped

    2 Pounds or less : $1.10

    Over 2 pounds but no more than 6 Pounds :$2.20

    Over 6 pounds but no more than 10 pounds : $3.70

    Over 10 pounds :$3.80

    The shipping charges per 500 miles are not prorated. For example, if a 2 pound package is shipped 550 miles, the charges would be $2.20. Write a program that asks the user to enter the weight of a package and then displays the shipping charges. "

    ps. i also have the user enter the miles and calculate that as well, here is what i have so far, i think i'm done except i'm getting some error and other thing

     import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    public class shipping
    { public static void main (String [] s)
    	String input;
    	int distance;
    	int distanceMultiplier = (distance / 500);
    	int distanceRemainder = (distance % 500);
    	double weight;
    	double total;
    	double rate;
    	DecimalFormat df = new DecimalFormat("#,##0.00");
    		input = JOptionPane.showInputDialog ("Enter the weight of the package in pounds");
    			weight = Double.parseDouble(input);
    		input = JOptionPane.showInputDialog ("Enter the distance the package will travel in miles");
    			distance = Integer.parseInt(input);
    			if (weight <= 2.0)
    			rate = 1.1;
    		else if (weight <= 6.0)
    			rate = 2.2;
    		else if (weight <= 10.0)
    			rate = 3.7;
    			rate = 3.8;
    			if (distanceRemainder == 0)
    			total = rate * distanceMultiplier;
    		else if (distanceRemainder != 0)
    			total = rate * (distanceMultiplier + 1);
    		JOptionPane.showMessageDialog (null, "Your shipping charges are $"  + df.format(total));
    after i do all of this i get a "variable distance might not have been initialized" and "variable total might not have been initialized"

    i tried initializing them by making them equal to 0 but when i run the program my final message dialog always comes up as $0.00

    does anyone know what i'm doing wrong?


  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999

    Re: need help debugging simple, beginner program

    For some reason, you calculate the distanceMultiplier and distanceRemainder before you ask for a distance value. I suggest you calculate distanceMultiplier and distanceRemainder after you have obtained a distance value to use.

    The error message about initialization appears because the compiler checks to see that all variables have a value before they are used. If a variable is only initialized inside an 'if' block, the compiler can't be sure it will have a value. Your 'if' block for the distanceRemainder check doesn't need an 'else if...' condition, you can use just an 'else', which will have the same result and will avoid the initialization error for 'total':
    if (distanceRemainder == 0) {
        total = rate * distanceMultiplier;
    else  {
        total = rate * (distanceMultiplier + 1);
    The purpose of computing is insight, not numbers...
    R. Hamming
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Mar 2011

    Re: need help debugging simple, beginner program

    omg, dude

    thanks a whole lot, i just figured the distance value would still be calculated since i assigned it in the beginning

    and thanks for explaining the error message, i completely understand it now

    while i was waiting for a response I actually was able to complete another program

    i've changed my code and everything works great now, thanks again

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

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.