0

Here is my method:

//usedLetters method to check if user's guess has already been guessed
private boolean usedLetters(char used[], char alphabet) throws IOException
{
   for(int x=0; x<used.length; x++){

    if(alphabet == used[x])
    {
        return true;
    }

    else
    {
        used[dataSize] = alphabet;
        return false;
    }
    }
}//End of usedLetters method

IT checks to see if the alphabet that the user entered in an another method has already been guessed. If it has already been guessed, it returns true, and if has not been already guessed, it adds the alphabet into used, and returns false. But the error says that there are no return statements...I am not familiar with methods in general, so I am confused. Any help would be appreciated!

6 Answers 6

6

What if used.length==0? Then the for-loop is never entered.

Therefore you need a return statement after the loop.

Sign up to request clarification or add additional context in comments.

Comments

2

What if the for is never entered? i.e. used.length == 0 (used is an empty array). In this case - nothing will be returned.

The compiler forbids a flow that can terminate without returning the value, and that's why it shows the error.


Also note, I believe even after fixing this issue - the program will yield a wrong result, it will only check the first element of used, without advancing to the next one.

Comments

2

You should move the return false; to just before the last }. Otherwise it will return in the first iteration of the loop.

Comments

0

/usedLetters method to check if user's guess has already been guessed
private boolean usedLetters(char used[], char alphabet) throws IOException
{
   for(int x=0; x

if(alphabet == used[x])
{
    return true;
}

else
{
    used[dataSize] = alphabet;
    return false;
}
}

}//End of usedLetters method

There should be return statement after for loop, currently there is no return statement that's why you are getting error.

Comments

0

As you have written your code, the method will always return on the first iteration of the cycle, while I doubt this is what you want.

I believe that you should return false outside the for. I am not sure what does to "guess an alphabet" mean but I think this is what you are trying to do.

2 Comments

Sorry..Guess an alphabet: I am making a hangman game and the user guesses an alphabet in a word. I am not sure what a boolean flag is...How do I make one
Oh, sorry got confused. The word in English for a symbol in a word is "letter". No offense meant just saying that replacing this in the statement will make it clearer
0

Yep, all the above are correct. You would be better off defining the return variable at the top, setting it to values wherever you need to within the method body, breaking out of the 'for' loop and returning it once at the end.

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.