0
import java.util.Scanner;
import java.util.Random;
import static java.lang.System.out;

class TestingStuf {
    enum tooWhat {tooHigh, tooLow, justRight};

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        Random myRandom = new Random();

        tooWhat guess;

        out.println("Pick a number between 1 and 10.");
        int userGuess = keyboard.nextInt(); 
        int randomNumber = myRandom.nextInt(10) + 1;

        if (userGuess < randomNumber) {
            guess = tooWhat.tooLow;
        }else if (userGuess > randomNumber) {
            guess = tooWhat.tooHigh;
        }else if (userGuess == randomNumber) {
            guess = tooWhat.justRight;
        }

        out.println("Your guess is:");

        if (guess == tooWhat.tooLow) {
            out.println("Too low.");

        }else if (guess == tooWhat.tooHigh) {
            out.println("Too high.");

        }else if (guess == tooWhat.justRight) {
            out.println("Correct!");

        }

            keyboard.close();
    }
}

In my code I have an error in the second set of "if" statements that says The "local variable guess may not have been initialized" even though in the previous "if" statement I give the "guess" variable a value that is dependent on the user input. What am I doing wrong?

4
  • it only gets assigned a value IF one of the cases assigns it one... What if none of them do? You would need to handle that in an else at the end of the if else's Commented Aug 25, 2016 at 22:57
  • 1
    Replace the last else if with else and it will work. Commented Aug 25, 2016 at 22:57
  • Or even just give it a default value to start with.... Commented Aug 25, 2016 at 22:57
  • you must initialize guess Commented Aug 25, 2016 at 23:01

3 Answers 3

2

If you look at your code, it seems like there's a path through the if...else if...else if where guess is never initialized. That's what the compiler is warning you about, because code subsequent to that expects that guess will definitely have a value.

Although we, as humans, know that your three conditions are mutually-exclusive, the compiler isn't quite as smart as we are. Just make your final one an else rather than an else if (...):

if (userGuess < randomNumber) {
    guess = tooWhat.tooLow;
}else if (userGuess > randomNumber) {
    guess = tooWhat.tooHigh;
}else { // *** No `if`
    guess = tooWhat.justRight;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Change your last if else statement to an else statement

 if (userGuess < randomNumber) {
        guess = tooWhat.tooLow;
    }else if (userGuess > randomNumber) {
        guess = tooWhat.tooHigh;
    }else 
        guess = tooWhat.justRight;

That way the compiler knows that there is no possibility that guess stays uninitialized.

Comments

0

Compiler isn't sure if variable guess shall ever be provided a value based on just going through if..else if..else..if ladder. If compiler were to evaluate values for each of these ladder steps it would be able to find that out but that would be an overkill for the compiler. Going by the code, replacing last else if with else shall resolve your issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.