简体   繁体   中英

Java Variable will not initialize in an if statement?

I have initialized and declared the variable "average." I do not understand why it won't compile. Does it have to do with the fact that it's in an if else? I've already tried "|| null" and that is not taking either. What to do?

 // declare variables
    Scanner keyboard = new Scanner (System.in);
    String given;
    String middle;
    String sur;
    String truefalse;
    boolean bool;
    int exam1;
    int exam2;
    int exam3;
    double average;

    // get input
    System.out.println("***************  Grade Computer *************");
    System.out.println("Enter the student's first name: ");
    given = keyboard.nextLine();

    System.out.println( "Enter the student's middle initial: ");
    middle = keyboard.nextLine();

    System.out.println( "Enter the student's last name: ");
    sur = keyboard.nextLine();

    System.out.println( "Enter EXAM 1 Grade: ");
    exam1 = keyboard.nextInt();

    System.out.println( "Enter EXAM 2 Grade: ");
    exam2 = keyboard.nextInt();

    System.out.println( "Enter EXAM 3 Grade: ");
    exam3 = keyboard.nextInt();

    keyboard.nextLine();
    System.out.println( "Bonus work completed [true/false]");
    truefalse = keyboard.nextLine();

    // adjust exam scores if necesssary
    if (truefalse == "true")
    {
        bool = true;
        exam1 = keyboard.nextInt();
        exam2 = keyboard.nextInt();
        exam3 = keyboard.nextInt();
    }
    else
    {
        average = ((exam1+exam2+exam3)/3);  
    }   

Why is the compiler saying "variable average might not have been initialized"?

You need to initialize average . The compiler is complaining because your code might or might not ever reach the else block. Yet, if you use average when it hasn't been initialized, you get an error.

double average = null;

Alternatively, you can initialize average in the if block as well, because if the if block runs and the else doesn't you'll still have an initialized average at the end of the day and the compiler is happy :).

if (truefalse == "true")
{
    average = //something;
    bool = true;
    exam1 = keyboard.nextInt();
    exam2 = keyboard.nextInt();
    exam3 = keyboard.nextInt();
}
else
{
    average = ((exam1+exam2+exam3)/3);  
}   

Also, as a final note, a good rule of thumb is to use .equals() for any String and == for ints, characters, booleans etc.

  • You declared your variable but did not strictly initialize it. It is only initialized if the logic of your program dictates that the line average = ((exam1+exam2+exam3)/3); is hit. This doesn't happen if truefalse == "true" .

  • In order to have Java compile it, you can do one of two things.

    1. Initialize your variable to some default value when you declare it, eg double average = 0;

    2. Make sure that no logical pathway precludes the possibility that the variable is initialized. That is, no matter what happens, it gets initialized.

The first option is better. It keeps your code cleaner and more logical, thus easier to maintain. The second option is more implicit than explicit. You probably shouldn't have to go walking through the logic yourself to see how it works out.

You can initialize the double variable average to 0 when you are declaring it.

double average=0;

Reason of this particular error is that you are trying to initialize the variable in if-else block. The variable will not be initialized if condition is not true. If you are using the variable further in your program, you will get run time errors. That is why Java compiler is letting you know that there are possibilities that the variable might be uninitialized

Furthermore it is always a good practice to initialize a local variable when you declare it.

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