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:

Dani AI

Generated

Two separate issues need addressing in the original thread: the compiler’s definite-assignment requirement for local variables (as hinted) and the program logic for selecting a price and computing the final bill. ’s quick fix (give the variable a default) works, but a clearer solution is to make your branching cover every possibility and compute the bill using dozens plus remainder eggs.

Reorder the thresholds from largest to smallest so each else-if tests a narrower remaining range; that avoids unreachable branches. Also decide whether the listed prices are per dozen or per egg. If they are per dozen, compute dozens * pricePerDozen + remainder * (pricePerDozen / 12) rather than multiplying price * eggs directly.

Example of a concise, safe approach (shows definite assignment, correct range checks, and correct billing):

import java.util.Scanner;

public class BuyingEggs {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter number of eggs: ");
    int eggs = in.nextInt();
    if (eggs < 0) { System.out.println("Invalid quantity"); return; }

    int dozens = eggs / 12;
    int extra = eggs % 12;

    double pricePerDozen;
    if (dozens >= 11) pricePerDozen = 0.35;
    else if (dozens >= 6) pricePerDozen = 0.40;
    else if (dozens >= 4) pricePerDozen = 0.45;
    else pricePerDozen = 0.50;

    double bill = dozens * pricePerDozen + extra * (pricePerDozen / 12.0);
    System.out.printf("Total: $%.2f%n", bill);
  }
}

Extra tips: validate nonnegative input, format currency with printf or DecimalFormat, and consider BigDecimal for production code to avoid floating-point rounding issues. This resolves the compiler concern and fixes the billing calculation while keeping logic readable and maintainable.

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.