1
public static String answer(boolean a, boolean b, boolean c, boolean d, boolean e)
{
    String response;
    if (a == true)
    {
        if (b == false)
        {
            if (c == true)
            {
                if (d == false)
                {
                    if (e == true)
                    {
                        response = "Your animal is a goose!";
                    }
                }
            }
        }
    }

    return response;
}

My problem is response. I want to return it to the main Tester, however it keeps saying it hasn't been initialized. I assume this means that it has been declared, right before the if else, but it's not recognizing the initialized variable inside it. how do I fix this?

This is written inside BlueJ, with Java. This program asks 5 questions (booleans a-e) to see which seven farm animals you are thinking of. The if statements determine your answers, and if they correspond, i want to return "response", which will print the animal you picked. Also, if there is a better way to do this, please inform me. I am new to Java, and coding, so any help is appreciated.

Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • 1
    If that assignment to `response` never happens, what should the function return? – user2357112 Dec 01 '14 at 04:08
  • There are seven animals for you to choose from, and you give the computer answers to the questions it asks. there will be six other scenarios, and if none of them fit, then it was user error. So, hypothetically speaking, response should always have an assignment (which I'm assuming means "initialized") I'm new to the coding world, so pardon any confusion I have given you. – Diamond Brook Dec 01 '14 at 04:10
  • Java won't allow your code unless it's sure that all variables are always initialized before use. User error happens. – user2357112 Dec 01 '14 at 04:21
  • so then should i just put response as null for all paths? except the final one – Diamond Brook Dec 01 '14 at 04:22
  • Please don't "update" the code in your question based on answers you have been given. It is confusing for future readers. – Radiodef Dec 01 '14 at 04:58

3 Answers3

0

The local variable (local inside a method) in Java has no default value. Thus, in order to use it, it must have a value.

Suppose that a is false, then what is the value that should be returned when reaching return response;? You must assign an initial value to it:

String response = null;
// or String response = "";
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
0

The compiler analyses all possible paths through your code, and it finds that it is possible that response is never assigned to, i.e. will not have been initialized.

You have to make sure that no matter how these if statements play out, response will have been assigned some value.

For example, by giving it a default to start out with

String response = "Your animal is not a goose!";

There are seven animals for you to choose from, and you give the computer answers to the questions it asks. there will be six other scenarios, and if none of them fit, then it was user error.

In this case, you should leave it uninitialized at first, and then have branches to reach those seven animals and an else that either throws an exception (for invalid input) or assigns an error message.

So, hypothetically speaking, response should always have an assignment

Well, in the code you showed, that's not the case.

If the code is very complex, the compiler may not be able to figure out that some paths are never reached, but in your case, if you add in all the animals, you should be able to get something that makes the compiler happy.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • I was thinking of having seven different blocks of if statements, not having one giant one with all them branching off boolean a. So, the compiler will run through each block. If the animal is not a goose, then response will still be null, and will run the next block, which for example, is a cow, and will see if all the booleans correspond with it (the booleans being the user answers.) It will keep doing this for all seven animals, until one of them has all the right booleans. Or should i just do one giant block, so all paths within the if else are met, and the compiler is happy? – Diamond Brook Dec 01 '14 at 04:20
  • " If the animal is not a goose, then response will still be null, and will run the next block, " Sure, that would work. However, you have to set `response=null` at the beginning. (The downside is that then the compiler does not check if you have all possible combinations of your booleans covered). – Thilo Dec 01 '14 at 04:28
  • my original problem still remains. It will always return null, because the response inside the if block, the final one, that says what your animal is, isn't accessible outside the if block. the return statement isn't recognizing it, and ignores it, and will always think it's null – Diamond Brook Dec 01 '14 at 04:30
  • You need to debug a little. If that assignment is reached, the String will be assigned and will be returned. I think we are exactly in the situation where you have some input that never reaches any assignment. – Thilo Dec 01 '14 at 04:32
  • what do you mean by that? you mean where the method isn't even being run? – Diamond Brook Dec 01 '14 at 04:33
  • In the code you posted, call the method with `(true,false,true,false,true)` and it will return the String. `null` for all other combinations. – Thilo Dec 01 '14 at 04:35
  • I'm going to edit the question now. Give me a second, and you will see what I was trying to do. I finished it while responding on here – Diamond Brook Dec 01 '14 at 04:38
0

Assuming that this is java, you need to specify

String response = null;

And you should be on your way!.

If the variable was on the global level then it would be assigned implicitly and automatically with a default value such as for a reference type the default would be null but for variables inside the methods, the programmer has to explicitly initialize them.

Please see this for a discussion on why local variables are not initialized automatically in java: Why are local variables not initialized in Java?

Community
  • 1
  • 1
Khanna111
  • 3,627
  • 1
  • 23
  • 25