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 For Each Loop

Michelle Miller
Michelle Miller
610 Points

./ScrabblePlayer.java:27; error: missing return statement -- why do I keep getting this?

When I enter my method for this challenge, I keep getting this error. I've fiddled with various approaches, but I can't figure out why I keep getting this error? I do have a return in my method but it says I don't!

Here is the full error I receive: ./ScrabblePlayer.java:27: error: missing return statement } ^ 1 error

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }

  public void addTile(char tile) {
    // Adds the tile to the hand of the player
    mHand += tile;
  }

  public boolean hasTile(char tile) {
   return mHand.indexOf(tile) > -1;
  }

  public int getTileCount() {
    int counter = 0;
    for (char letter: mHand.toCharArray()) {
      counter++;
      return counter;
    }
  }
}

1 Answer

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,074 Points

Your method "getTileCount" has to have a char parameter. So you can check how many times that paremeter that you have received appears in the mHand. You can do it like this:

  public int getTileCount(char tile) {
    int counter = 0;
    for (char letter: mHand.toCharArray()) {
      if (letter == tile) {
      counter++;
      }
    }
    return counter;
  }
Michelle Miller
Michelle Miller
610 Points

Thank you! I think I see my error. I misunderstood the instructions to be asking for a count of how many tiles, total, were in mHand.

However, it looks like the instructions are asking how many of a specific tile (as in the parameter) is in mHand. So your solution makes complete and total sense.

Although I have to confess, I'm still confused as to why I received the error I did for my own method? Should my own method not throw an error, even if it might get rejected because it doesn't provide the solutions the instructions request?

Michelle Miller
Michelle Miller
610 Points

Thank you! I think I see my error. I misunderstood the instructions to be asking for a count of how many tiles, total, were in mHand.

However, it looks like the instructions are asking how many of a specific tile (as in the parameter) is in mHand. So your solution makes complete and total sense.

Although I have to confess, I'm still confused as to why I received the error I did for my own method? Should my own method not throw an error, even if it might get rejected because it doesn't provide the solutions the instructions request?