Switch statement resulting in java:240 (might not have been initialized)

无人久伴 提交于 2019-12-02 08:23:37

If pepperoni is not Y, y, N, or n, you never assign a value to topping1, because the default case never assigns it a value. E.g., if pepperoni is not one of those four values, then the flow of control skips the other two cases and goes to default, which never gives topping1 a value, so later when you try to use it, it's possible topping1 has never received a value at all.

The "workaround" is to correct the logic so that you never try to use topping1 without having assigned it a value. How you do that depends on logic you haven't shown us. You might assign it a value other than 0 or 1 (the values you assign in the other branches of the switch), for instance.

The issue probably is that the switch statement does not guarantee a value set for topping1. If you received a response of 'L' you would neither set it to 1 or 0. You should set a default value when you initialize topping1 or set one in the default clause.

Java's compiler can't analyze your code to know that you won't let people out of the loop (that I presume this is in) until it's set. It can only tell that there's a path through the code which would allow it to not be set.

This works (same would be true for switch):

int a;
if (condition()) {
  a=0;
} else {
  a=1;
}
System.out.println(a);

And this works:

int a=1;
if (condition()) {
  a=0;
} 
System.out.println(a);

This does not:

int a;
if (condition()) {
  a=0;
} 
System.out.println(a); // compiler error!

because if condition() returns false, a is undefined. Local variables must be defined. Note that this is different than fields on classes which automatically are assigned default values of null, 0 or false.

You are getting the "might not have been initialized" error because there is a path through your switch statement in which you don't initialize topping1 and you reference the variable later.

What you can do: Initialize topping1 to an invalid value (say, -1). Then place your case statement in a while loop that checks if the value of topping1 is still equal to -1.

Then, once you are out of the while loop, you know that the following are true:

  1. You have intialized topping1, so no compiler error should result.
  2. You have a valid value for topping1.

This looks a little ugly, but writing the loop like this is one way to stop the compiler complaining:

for (;;) {
    System.out.print("Do you want Pepperoni? (Y/N): ");
    pepperoni = scan.next().charAt(0);
    switch (pepperoni) {

        case 'Y':
        case 'y':
            topping1 = 1;
            break;

        case 'N':
        case 'n':   
            topping1 = 0;
            break;

        default: 
            System.out.println("This is not a valid response, please try again");
            continue;
    }
    break;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!