0

Why is this function complaining that it should return a result of type char?

public static char getUserResponseToGuess(int guess){
    Scanner input = new Scanner(System.in);     
    char userResponse = 'x';            
    while((userResponse != 'h' && userResponse != 'l' && userResponse != 'c')){
        System.out.print("Is it" + guess + "? (h/l/c)");        
        userResponse = input.next().charAt(0);
        if (userResponse == 'h' || userResponse == 'l' || userResponse =='c'){
            return userResponse;            
        }
        else{
            System.out.print("Is it" + guess + "? (h/l/c)");
        }
    }
}

3 Answers 3

2

The static analysis done by the compiler does not understand the logic you have used, so based on its analysis all the possible flows does not end up returning a value

When the compiler analysis the code, if the if condition in the code is not satisfied and the loop is exited then the method won't return a value.

public static char getUserResponseToGuess(int guess){
    Scanner input = new Scanner(System.in);     
    char userResponse = 'x';            
    while((userResponse != 'h' && userResponse != 'l' && userResponse != 'c')){
        System.out.print("Is it" + guess + "? (h/l/c)");        
        userResponse = input.next().charAt(0);
        if (userResponse == 'h' || userResponse == 'l' || userResponse =='c'){
            return userResponse;            
        }
        else{
            System.out.print("Is it" + guess + "? (h/l/c)");
        }
    }
    return userResponse;  
}
Sign up to request clarification or add additional context in comments.

5 Comments

If you help the compiler a bit (e.g. by doing a while (true)) it also does not complain.
@vanza ... could you please expand a little on this concept?
A while (true) loop never finishes, unless you explicitly do it with a return or a break. The compiler sees that the only way out of your loop is a return, so it doesn't complain about the lack of a return value.
Oh wow. I'm kind new to Java. I didn't know a return statement also would get me out of the loop. Good to know. ;)
Now that Im thinking about this is kind what Arun is doing.
1

What happens when the userResponse is not one of those three characters? It will exit the loop and return what? Every path has to return a char

5 Comments

When it's not one of those characters the code will execute the return statement inside the loop.
Oops, the opposite. If it's not one of those, it will remain inside the loop until it is, then it executes the return statement.
The compiler isn't smart enough to walk through all the logic and know that it must return.
that's true, but that's not what your answer says. Your answer assumes his code is wrong, which is not the case; it's just the compiler that's not smart enough to understand it.
Well, that's what the question was about: why doesn't it compile. It doesn't compile not because it's logically wrong, but because the compiler isn't smart enough. There is a difference between those two.
0

This worked as it should. Arun P Johny solutions also works.

public static char getUserResponseToGuess(int guess){
    Scanner input = new Scanner(System.in);     
    char userResponse = 'x';            
    System.out.print("Is it " + guess + "? (h/l/c): ");
    while((userResponse != 'h' && userResponse != 'l' && userResponse != 'c')){             
        userResponse = input.next().charAt(0);
        if (userResponse == 'h' || userResponse == 'l' || userResponse =='c'){
            break;                          
        }
        else{
            System.out.print("Is it " + guess + "? (h/l/c): ");
        }
    }
    return userResponse;    
}

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.