-2

When I try to compile, it keeps giving me and error saying that the "total" variable is not initialized. I know the error is at the last line of code at

System.out.println(("The charges are ") + money.format(total) + "\n");

Here's my full code. I tried to format it as best as I could.

import java.util.Scanner;

import java.text.DecimalFormat;

public class Internet

{

public static void main(String[] args)
{

    String packageType;                        //string to get type of package bought
    char packageLetter;                        //char of converted string
    final double PACKAGE_A_PRICE = 9.95;       //Package A base price
    final double PACKAGE_A_HOURS_PRICE = 2.25; //Package A additional hours price
    final int PACKAGE_A_MIN_HOURS = 10;        //Package A minimun hours
    final double PACKAGE_B_PRICE = 14.95;      //Package B base price
    final double PACKAGE_B_HOURS_PRICE = 1.10; //Package B additional hours price
    final int PACKAGE_B_MIN_HOURS = 20;        //Package B minimun hours
    final double PACKAGE_C_PRICE = 22.95;      //Package C base price
    double hoursUsed;                          //variable for number of hours used
    final int HOURS_MIN = 0;                   //minimum hours of service use
    final double HOURS_MAX = 744;              //maximun hours of service use
    double overusedHours;                      //variable for additional hours
    double total;                              //variable for the total price
    DecimalFormat money = new DecimalFormat("$0.00"); //US currency format

    //read input
    Scanner keyboard = new Scanner(System.in);

    //get package type, convert to char, then captitalize it
    System.out.print("Which plan <A, B, or C>? ");
    packageType = keyboard.nextLine();
    packageLetter = Character.toUpperCase(packageType.charAt(0));

    //case switch to check if package input is valid
    switch (packageLetter)
    {

        case 'A':
            //number of hours & check if input is valid
            System.out.print("How many hours? ");
            hoursUsed = keyboard.nextDouble();
            if (hoursUsed < 0 || hoursUsed > 744)
                System.out.printf("Hours must be between %d and %.1f. You entered %.1f.\nProgram ending\n",HOURS_MIN,
                    HOURS_MAX, hoursUsed);

            //calculate total
            overusedHours = hoursUsed - PACKAGE_A_MIN_HOURS;
            if (overusedHours < 0)
                overusedHours = 0;
            total = PACKAGE_A_PRICE + (overusedHours * PACKAGE_A_HOURS_PRICE);
            break;

        case 'B':
            //number of hours & check if input is valid
            System.out.print("How many hours? ");
            hoursUsed = keyboard.nextDouble();
            if (hoursUsed < 0 || hoursUsed > 744)
                System.out.printf("Hours must be between %d and %.1f. You entered %.1f.\nProgram ending\n",HOURS_MIN,
                HOURS_MAX, hoursUsed);

            //calculate total
            overusedHours = hoursUsed - PACKAGE_B_MIN_HOURS;
            if (overusedHours < 0)
                overusedHours = 0;
            total = PACKAGE_B_PRICE + (overusedHours * PACKAGE_B_HOURS_PRICE);
            break;

        case 'C':
            total = PACKAGE_C_PRICE;
            break;

        default:
            System.out.printf("Package must be A, B, or C. You entered %s.\n",packageType +
                "\nProgramming ending");


    }
    System.out.println(("The charges are ") + money.format(total) + "\n");
}

}

  • 3
    This error message is telling you **exactly** what is wrong, and this should lead you to figure out how to fix it. Consider initializing the total variable to some default value when you declare it. e.g., `double total = 0.0;` – Hovercraft Full Of Eels Feb 12 '15 at 22:56
  • There really isn't much to say here. Re-read the error. It doesn't say it hasn't been initialized, it's saying it _might_ not have been, as in: you're using it at a place where it might not have received an initial value. – keyser Feb 12 '15 at 22:58

2 Answers2

0

The message is pretty straight forward. Simply put, your variable total might not ever get a value if the switch-case function turns to default behaviour. (Meaning packageLetter was not A, B or C.)

When the code later tries and access the variable, you get this.

One solution would be to just set the variable to 0 when declaring it.

double total = 0;
Robin Jonsson
  • 2,761
  • 3
  • 22
  • 42
0

It may not have been initialized because it mihgt not have hit the conditions. Just assign it a default value of 0 where you declare and the problem is fixed

engineercoding
  • 832
  • 6
  • 14