I am writing a program that calculates the bill when buying eggs by the dozen. I have for the most part figured out most of the code but, I am getting an error message that is saying "variable price might not have been initialized.
Here is the code please help me.

public class BuyingEggs
{
public static void main(String[] args)

{
int eggs; //number eggs to purchase
int dozen; //how many dozen in egg purchase
double price; //price of eggs
double bill; //total cost of purchase
double extra; // how much extra eggs cost

System.out.print("Enter number of eggs: ");
eggs = Input.readInt();

dozen = eggs/12;


if (dozen >= 11)
{
price = 0.35;
extra = 0.029;
}
else if (dozen < 11)
{
price = 0.40;
extra = 0.033;
}
else if (dozen < 6)
{
price = 0.45;
extra = 0.037;
}
else if (dozen < 4)
{
price = 0.50;
extra = 0.041;
}

bill = price * eggs;

System.out.println("Enter number of eggs: " + eggs);
System.out.println("Your cost is $ " + price);
System.out.println("Your bill comes to $" + bill);
System.out.println();
System.out.println("end of program");
} //end main
} //end class BuyingEggs


Thank You in advance!!!! :lol:

Recommended Answers

All 4 Replies

Try

double price=0.00;

-Tino

I am writing a program that calculates the bill when buying eggs by the dozen. I have for the most part figured out most of the code but, I am getting an error message that is saying "variable price might not have been initialized.
Here is the code please help me.

public class BuyingEggs
{
public static void main(String[] args)

{
int eggs; //number eggs to purchase
int dozen; //how many dozen in egg purchase
double price; //price of eggs
double bill; //total cost of purchase
double extra; // how much extra eggs cost

System.out.print("Enter number of eggs: ");
eggs = Input.readInt();

dozen = eggs/12;


if (dozen >= 11)
{
price = 0.35;
extra = 0.029;
}

else if (dozen < 11)
{
price = 0.40;
extra = 0.033;
}
else if (dozen < 6)
{
price = 0.45;
extra = 0.037;
}
else if (dozen < 4)
{
price = 0.50;
extra = 0.041;
}

bill = price * eggs;

System.out.println("Enter number of eggs: " + eggs);
System.out.println("Your cost is $ " + price);
System.out.println("Your bill comes to $" + bill);
System.out.println();
System.out.println("end of program");
} //end main
} //end class BuyingEggs


Thank You in advance!!!! :lol:

BrownSuga,

I slightly modified your post's topic to make it more polite. "Need Help Quick!!!" implied that you deserve precedence over someone else's post, which is kind of considered poor etiquette.

Just letting you know for future reference, being your first post and all. Additionally, creating a post title relevant to your issue also helps attract people who might be able to help.

As a general rule, Class primitive type variables are automatically initialized to a default value.

However if the variable is declared in a method (as in your case,) the value must be initialized prior to use.

Even though you set the price to a value, they are all done within the 'if' and 'else if' constructs. Since the compiler determined that there is a possibility that the conditions will not be met (because you never include and 'else' clause by itself,) it assumes that there is a possibility that 'price' will never be initialized. Even though you take into account all possibilities in your ‘if’ statements, the compiler can’t determine all possible outcomes.

This is why you received the error that the price variable 'might' not be initialized.

Thank You all so much. I was getting very frustrated about that. I got an A on my program!!

Thank You All Again!! :)

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.