Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Creating the MVP Prompting for Guesses

Dorka Tamas
Dorka Tamas
5,085 Points

missing return statement }

Hi,

I tried to perform the task as it is in the video, but I got this message:

Game.java: 19: missing return statement }

I think I have all the curly brackets, so I am not sure what the problem is.

Thanks in advance!

Dorka Tamas
Dorka Tamas
5,085 Points

This is my code:

public class Game { private String mAnswer; //What is a member variable? private String mHits; private String mMisses;

public Game(String answer) { //is this a constructor? - for forcing, dont understand why it is like this
    mAnswer = answer;
    mHits = "";
    mMisses = "";
}

public boolean applyGuess(char letter) {
    boolean isHit = mAnswer.indexOf(letter) >= 0;
    if (isHit) {
        mHits += letter;
    } else {
        mMisses += letter;
    }
}

}

3 Answers

Game.java: 19: missing return statement

So the error message is telling you that there is no return statement.

Check the applyGuess method, you've set the return type of that method to boolean, so this method should return a boolean. By the looks of it, i believe you want to return the isHit boolean, check the snippet below.

public boolean applyGuess(char letter) {
    boolean isHit = mAnswer.indexOf(letter) >= 0;
    if (isHit) {
        mHits += letter;
    } else {
        mMisses += letter;
    }
    return isHit;
}

Happy coding & Good luck! :-)

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

It looks like this code is supposed to return a boolean. But you're not actually returning anything. Try either making it void or return a boolean.

public boolean applyGuess(char letter) {
    boolean isHit = mAnswer.indexOf(letter) >= 0;
    if (isHit) {
        mHits += letter;
    } else {
        mMisses += letter;
    }
}

IE you may need a return statement to return the isHit value.

Dorka Tamas
Dorka Tamas
5,085 Points

Thanks. It works now. :)