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

与世无争的帅哥 提交于 2019-12-20 07:28:35

问题


I am trying to store a value inside a variable depending on the input:

switch(pepperoni) {

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

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

    default: 

        {
    System.out.print("This is not a valid response, please try again \n");  
    System.out.print("Do you want Pepperoni? (Y/N): ");
    pepperoni = scan.next().charAt(0);
        break;
    }

I want the variable topping1 to store the value 1 if the input is 'Y' or 'y' and to store the value 0 if the input is 'N' or 'n'

If the input is neither 'Y', 'y', 'N' nor 'n' then I want it to repeat the question until a valid input is typed in.

The problem arises when I later in the program try to print the value 'because it might have not been initialized', which somewhat makes sense. (example below)

if(topping1 > 0)
    System.out.println("Pepperoni"); 

// 243: error: variable topping1 might not have been initialized

I do realize there are other ways to do this, but as I am really wanting to learn Java I try to understand as much of the fundamentals as possible. Therefore would I be really happy if someone could tell me why this not work and if there is a way to do this with a switch statement or quick fixes.


回答1:


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.




回答2:


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.




回答3:


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.



回答4:


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


来源:https://stackoverflow.com/questions/15147639/switch-statement-resulting-in-java240-might-not-have-been-initialized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!