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

Adia Gawhari
Adia Gawhari
1,378 Points

Error : missing return statement

I keep getting this error: missing return statement }. But I'm sure that I have closed all my curly brackets.

public class Game { private String mAnswer; private String mHits; private String mMisses;

public Game(String answer) { mAnswer = answer; mHits = ""; mMisses = ""; }

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

public class Game { 
  private String mAnswer; 
  private String mHits; 
  private String mMisses;

public Game(String answer) { 
  mAnswer = answer;
   mHits = "";
    mMisses = ""; 
  }

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

This is more readable And here is everything about return statements......easy google search https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

BTW: you should use foreach to search for letters, that is far more common

2 Answers

Rohit Tolawat
Rohit Tolawat
8,277 Points

Hi Adia, Although I am not sure of what you are trying to achieve, but one of the issue I found from the code is as below: public boolean applyGuess(char letter) method has a return type of boolean. But you don't seem to return any boolean in that method. If you don't want to return anything, change the return type as void in the method signature.

Hope it solves the issue highlighted.

dusanveljkovic
dusanveljkovic
1,035 Points

you are missing return statement on the end.

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

return isHit;

} }

Adia Gawhari
Adia Gawhari
1,378 Points

Thank you all so much!