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");
}
}