简体   繁体   中英

Eclipse, The local variable cannot be initialized

Ive been working on this program for a few days now and its due tonight at midnight. I cannot for the life of me figure out why I keep getting a " The local variable cannot be initialized" error. This is my first coding class and I do not understand it very well. If someone could help me out by explaining a fix and why this error keeps happening that would be great.

I have put "**" where the errors are ( near the end of the code). Any help would be great! Thanks in advance.

/*This program will determine how much the students 
tuition and fees are based on location and classes. It will return the
total tuition, fees, and combined total */
import java.text.DecimalFormat;
import java.util.*;

public class Project2 {

public static void main(String[] args) {
    Scanner in=new Scanner(System.in);

    //Declaring variables and decimal format
    int TotalHours;
    int Price;
    int CreditCharge;
    int CITFee;
    int OnlineFee;
    int INFCSCFee;
    int TotalTuition;
    int TotalFee;
    int TotalCombined;
    DecimalFormat df = new DecimalFormat("$#,###");


    //Getting the students First name, Last name, and Date
    System.out.print("Enter your first name: ");
    String FirstName = in.nextLine();
    System.out.print("Enter your last name: ");
    String LastName = in.nextLine();
    Date d = new Date ( );


    //Getting the state of residency. If in Ohio, asking the user if they are Metro
    System.out.print("Enter your State of residency as a 2-letter abbreviation: ");
    String State = (in.next().toLowerCase());

    if (State.equals ("oh") || State.equals("OH")){
        System.out.print( "Are you a Cincinnati resident? (Y/N) ");
        String Metro = in.next();    
        if (Metro.equals ("y")) Price = 567;    
        }
    else Price = 750;

    if (State.equals ("ky") ){ Price = 375; 
        }
        else if (State.equals ("in")){Price = 375;  
        }
        else {Price = 750;
        }       

    //Getting the number of credit hours the student is taking
    System.out.print("Enter the total credit hours for the upcoming semester: ");
     TotalHours = in.nextInt();

    if (TotalHours <= 12) CreditCharge = (TotalHours * Price);

    else {CreditCharge = (Price * 12);
    }    

    //Getting the number of CIT hours the student is taken
    System.out.print("Enter the total of CIT credits you are taking: ");
    int TotalCITHours = (int) in.nextInt();
    CITFee = (TotalCITHours * 40);

    //Getting the number of online credit hours the student is taken
    System.out.print("Enter the total number on-line credit hours you are         taking: ");
    int OnLine = (int) in.nextInt();
    OnlineFee = (OnLine * CITFee * 35);

    //Seeing if the student is taken either INF 120 or CSC 260 
    System.out.print("Are you taking either INF 120 or CSC 260? (Y/N) ");
    String INFCSC = in.next().toLowerCase();
    if (INFCSC.equals ("y")) INFCSCFee = (char) (CITFee * OnlineFee + 60);

    //Calculating the tuition, fees, and total combined.
    **    TotalTuition = CreditCharge;
    **    TotalFee = INFCSCFee;
    **    TotalCombined = TotalTuition + INFCSCFee;       

    //Tuition Statement for FirstName, LastName, Date
    System.out.println("\nTuition Statement for " + FirstName + LastName);
    System.out.println(d);
    System.out.println("Tuition: " + df.format (TotalTuition) );
    System.out.println("Fees: " + df.format(TotalFee));
    System.out.println("Total: " + df.format(TotalCombined));

    }

}

Your variable INFCSCFee was not initialized if they did not answer yes to the question "Are you taking either INF 120 or CSC 260? (Y/N) ". Eclipse will not let you run the program if a variable may not have been initialized. At the top of your code where you have

int INFCSCFee;

replace it with

int INFCSCFee = 0;

or initialize the variable somewhere else or with some other value.

Here is the fault

    if (INFCSC.equals ("y")) INFCSCFee = (char) (CITFee * OnlineFee + 60);

This is the only place INFCSCFee can be initialised. So it is possible therefore that it has not been initialised by the time it is used. More generally, this is not allowed:

int x; // declare x - not yet initialised
if (someCondition)
   x = 3; // initialise to 3
// if somecondition was false, x is still unitialised
System.out.println("x is "+x); // Error

You cannot use a variable declared in a method unless the compiler can gaurantee it has been initialised beore you us it. This would be allowed:

int x; // declare x - not yet initialised
if (someCondition)
   x = 3;
else if (somethingElse)
   x = 4;
else
   x = 5;

// For all outcomes, x has been initalised, so it is safe to use
System.out.println("x is "+x);

This is allowed too:

int x; // declare x - not yet initialised
if (someCondition)
   x = 3;
else
   return;

// Although someCondition may have been false, if we reach this line of
// code, it is because someCondition was true and therefore x was initialised
System.out.println("x is "+x); // will print x is 3

Finally, you can initalise your variable at declaration time:

int x = 0;

Now, wherever x is used, it is gauranteed to be initalised, so you won't get a compile error. That's not necessarily a good thing, because that compiler error could actually be showing you a bug in your code, and you've suppressed it by giving the variable a default value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM