Hello I am an absolute begginer and am working on a paint calulator program. I am trying to return the price from the paintformula ex: price = paintFormula(wallArea,price,height,length,width); or paintFormula(wallArea,price,height,length,width);. When I call the method I keep getting errors like: PaintCalculator2.java:33: error: variable price might not have been initialized.

import java.util.Scanner;

public class PaintCalculator2
{
     public static void main(String [] args)
     {
      Scanner keyboard = new Scanner(System.in);

      double wallArea;                            
      double height; 
      double length; 
      double width;
      double price;
      double WallArea;
      double paintQuantity;



       //Prompts user for the dimensions of the room                         
        System.out.print("Please enter the height of the room:   ");
        height = keyboard.nextDouble();

        System.out.print("Please enter the length of the room:   ");
        length = keyboard.nextDouble();

        System.out.print("Please enter the width of the room:   ");
        width = keyboard.nextDouble();

        wallArea = wallAreaMethod(height, length, width);

        System.out.println("So the wall area is " + wallArea + "ft.");

        price = paintFormula(wallArea,price,height,length,width);
       }



            //Calulates the area of the wall in a room
            public static double wallAreaMethod(double height, double length, double width)
            {
             double wallArea;
             wallArea = 2*length*height + 2*width*height;

             return wallArea;
            }



               //Computes the quanity of paint needed

               public static double paintFormula(double areaMethod, double price, double height, double length, double width)
              {

               areaMethod = wallAreaMethod(height, length, width);

               double paintQuantity;

               paintQuantity = areaMethod * 2 / 350;


               price = paintQuantity * 32.0;

               return price; 
              }


}

Recommended Answers

All 5 Replies

Define you Price variable like this instead:

double price = 0.0;

The problem is that double price; sets the value to null, or at the very least; your IDE thinks it does. By setting a default value manually, you'll prevent the warning from being displayed.

I should have read all your code, as you have a bit strange arguments for your paintFormula method. You do know that you don't have to define variables as a method argument?

Rewrite the method like this:

public static double paintFormula(double height, double width, double length)
{
    double areaMethod = wallAreaMethod(height, length, width);
    double paintQuantity = areaMethod * 2 / 350;

    // either define a new varibale and return this:
    double _price = paintQuantity * 32.0;
    // return _price;
    // or just return the value directly:

    return paintQuantity * 32.0;
}

Hope this helps?

Thank you for the quick responce. This helps a lot.

This thread doesn't add up.
"price" is an instance variable. The language defines that it will have a default initial value of 0.0 All instance variables are initialised to their defined default values (0, 0.0, false or null as appropriate) when the instance is first created. (NB as a primitive, the double price can never be null)
The error message in the first post is seen with a local variable in a method - local variables do not have default values, so must be explicitly initialised.

JamesCherrill: price an instance variable ? it's declared within the main method, just like all the other variables.
where do you see it as an instance variable ?

Aaaaaaaarrrrggggghhhhhh
Stupid me, my mistake, didn't read it properly. Thanks for picking up on that. Aplogogies to everybody.

<excuse> I'm in the middle of switching everything back to Macs after 12 years in the Windows jungle, and that was distracting me </excuse>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.