1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Rep Power

    Can not get the variable to initialize

    Hi all,
    This is homework and I am not looking for a cheat but I have tried everything that I can think of to get this program running. I am using the latest JGrasp IDE. When I compile this it gives me the error: ISP.java:62: error: variable totalCharges might not have been initialized
    System.out.println("Your charge this month is: $" + totalCharges);
    1 error

    I tried giving it a value of "" and even 0.00 but it seems like the if statements are just being skipped.

    Also when I run this, it will show me the last else output statement regardless if one of the previous if statements are true. The book I am using is very vague on this problem and I am wondering if anyone here can help me out with some suggestions.
    import java.util.Scanner;
    public class ISP
    	public static void main(String[] agrs)
    		// Declare variables
    		double packageAPrice = 9.95;
    		double packageBPrice = 13.95;
    		double packageCPrice = 19.95;
    		double aHoursOverPrice = 2.00;
    		double bHoursOverPrice = 1.00;
    		int hoursUsed;
    		String packageChoice;
    		double totalCharges;
    		// Create Scanner object to input data
    		Scanner keyboard = new Scanner(System.in);
    		// Prompt user to input their package choice
    		System.out.print("What ISP package did you choose? ");
    		packageChoice = keyboard.nextLine();
    		// Prompt user to input their hours used for the month
    		System.out.print("How many hours did you use this month? ");
    		hoursUsed = keyboard.nextInt();
    		// Initiate if else statements
    		if (packageChoice.equals('A') || packageChoice.equals('a'))
    			if (hoursUsed > 10)					
    				totalCharges = packageAPrice + ((hoursUsed - 10) * aHoursOverPrice);
    				totalCharges = packageAPrice;
    		else if (packageChoice.equals('B') || packageChoice.equals('b'))
    			if (hoursUsed > 20)
    				totalCharges = packageBPrice + ((hoursUsed - 20) * bHoursOverPrice);
    				totalCharges = packageBPrice;
    		else if (packageChoice.equals('C') || packageChoice.equals('c'))
    			totalCharges = packageCPrice;
    			System.out.println("You must enter either Package A, B, or C");
    		System.out.println("Your package choice is: " + packageChoice);
    		System.out.println("Your usage this month was: " + hoursUsed);
    		System.out.println("Your charge this month is: $" + totalCharges);
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Rep Power

    You only assign totalCharges in the "if" parts, not in the last "else" part. So it's possible that it hasn't been assigned at the end of the big "if" statement (even though you want to use it). That's what Java complains about. And this is actually a very helpful message, because you have somewhat of a logical error in your code: If the user input is invalid, then obviously you do not want to show the charge but rather ask for new input, throw an exception or so.

    Secondly, you're confusing chars and Strings: 'A' with single quotes is a char. What you want is the String "A" with double quotes. And you should actually replace the two "equals" checks with a single "equalsIgnoreCase" check.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo