Some Java Advice . . .

Discussion in 'Mac Programming' started by sixstorm, Feb 13, 2007.

  1. macrumors regular

    #1
    Hello fellow programmers. I just wrote this super simple program and for some reason, JGrasp is having a hard time with my discount variables, saying "they may not have been initialized". Check the code out for yourself. I'm sure I'm just missing something but . . .

    Also, the letters in bold are the letters that JGrasp is pointing out.

    Code:
    /* Jonathan Andrew Scott
    	2/13/07
    	jscottpass4.java
    	This program is for a catering service in order to figure up the totals upon which the user
    	will input.  See comment below for more details.
    */
    
    import javax.swing.JOptionPane;
    
    public class jscottpass4
    {
    	// Constants
    	public static final double BPRICE = 5.50;
    	public static final double	LPRICE = 9.50;
    	public static final double	DPRICE = 16.50;
    	public static final double	BDISCOUNT = .10;
    	public static final double	LDISCOUNT = .15;
    	public static final double DDISCOUNT = .12;
    
    	public static void main(String[] Args)
    	{
    		// Variables
    		double bfastIn, lunchIn, dinnerIn, totalBf, totalL, totalD, beforeTax, totalTax, afterTax;
    		double bDiscount, lDiscount, dDiscount;
    		String bfastStr;
    		String lunchStr;
    		String dinnerStr;
    		String totalOutputStr;
    		
    		bfastStr = JOptionPane.showInputDialog("Enter Number of Breakfast Ordered: ");
    		bfastIn = Double.parseDouble(bfastStr);
    		
    		lunchStr = JOptionPane.showInputDialog("Enter Number of Lunches Ordered: ");
    		lunchIn = Double.parseDouble(lunchStr);
    		
    		dinnerStr = JOptionPane.showInputDialog("Enter Number of Dinners Ordered: ");
    		dinnerIn = Double.parseDouble(dinnerStr);
    		
    		// If statement to check if there is a discount or not
    		// Breakfast Discount Check
    		if (bfastIn >= 10)
    			{
    				bDiscount = (BPRICE * bfastIn) * BDISCOUNT;
    				totalBf = (BPRICE * bfastIn) + bDiscount;
    			}
    		else
    			{
    				totalBf = BPRICE * bfastIn;
    			}
    			
    		// Lunch Discount Check	
    		if (lunchIn >= 15)
    			{
    				lDiscount = (LPRICE * lunchIn) * LDISCOUNT;
    				totalL = (LPRICE * lunchIn) + lDiscount;
    			}
    		else
    			{
    				totalL = LPRICE * lunchIn;
    			}
    			
    		// Dinner Discount Check
    		if (dinnerIn >= 8)
    			{
    				dDiscount = (DPRICE * dinnerIn) * DDISCOUNT;
    				totalD = (DPRICE * dinnerIn) + dDiscount;
    			}
    		else
    			{
    				totalD = DPRICE * dinnerIn;
    			}
    			
    		// Calculations for totals
    			
    		beforeTax = totalBf + totalL + totalD;
    		totalTax = beforeTax * .10;
    		afterTax = beforeTax + totalTax;
    		
    		// Output Message for All Information
    		
    		totalOutputStr = "Meal   " + "Quantity   " + "Cost  " + "Discount  " + "Cost After Discount\n" +
    							  "Breakfast" + bfastIn + " " + BPRICE + " " + bDiscount + " " + totalBf + "\n" +
    							  "Lunch" + lunchIn + " " + LPRICE + " " + lDiscount + " " + totalL + "\n" +
    							  "Breakfast" + dinnerIn + " " + DPRICE + " " + dDiscount + " " + totalD + "\n";
    		
    		// Display the dialog for final information.
    					  
    		JOptionPane.showMessageDialog(null, totalOutputStr, "Totaled Information", JOptionPane.INFORMATION_MESSAGE);
    		
    		System.exit(0);
    	}
    }
    
    Thanks in advance!
     
  2. macrumors 603

    jeremy.king

    #2
    change
    Code:
    double bDiscount, lDiscount, dDiscount;
    to
    Code:
    double bDiscount=0.0;
    double lDiscount=0.0;
    double dDiscount=0.0;
    
    The moral of the story is these variables are only set in the true part of your if block, and are referenced outside of your if conditions in totalOutputStr. You could also assign a value to these variables in the else block.

    PS - It is convention to capitialize (w/mixed case) your class names. jscottpass4 should become JScottPass4.
     
  3. macrumors regular

    #3
    Thanks for the reply. It's a little weird that you have to set those to a default 0.0 value, but I guess my C++ and Java are running together. Thanks again!
     
  4. macrumors 603

    jeremy.king

    #4
    You always have to initialize variables within a method.

    Local variables do not get default values, but class instance and static variables will get a default value assigned.
     

Share This Page