initialization – Java variable may not have been initialized

initialization – Java variable may not have been initialized

Instance variables (in your case, they would be integers) are assigned to 0 be default. Local variables not. (From Java Docs)

If the loop is not entered, then your variables wont be initialized, thats the reason of the error.

What you can do is initialize them when declaring:

int a=0, b=0, c=0;

Your problem is this line:

System.out.println(( + a + ,  + b + ,  + c + ));

which is after the while (!answerFound) {...} loop. The compiler thinks that there may be a case where one or more of the variables a, b or c isnt initialised.

Use this line:

int a=0, b=0, c=0;

when declaring the variables, so that they are initialised when declared, and the error should go away.

initialization – Java variable may not have been initialized

Do this, at the beginning of the method after the local variables are declared:

a = b = c = 0;

The error is basically stating that Java cant be sure that the variables have a value assigned when they reach the System.out.println(). Remember: in Java only attributes have default values, all local variables must be explicitly initialized at some point.

Leave a Reply

Your email address will not be published. Required fields are marked *