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 Creating the MVP Scrabble Tiles

I am completely lost on this. I get a compile error "missing return statement"

Java Objects Challenge Task 2 of 2. I have no clue what to do here.

ScrabblePlayer.java
public class ScrabblePlayer {
  // A String representing all of the tiles that this player has
  private String tiles;

  public ScrabblePlayer() {
    tiles = "";
  }

  public String getTiles() {
    return tiles;
  }

  public void addTile(char tile) {

    tiles += "tile";
    // TODO: Add the tile to tiles

  }

  public boolean hasTile(char tile) {
    // TODO: Determine if user has the tile passed in

boolean isTile = tiles.indexOf(tile) != -1;
    if(isTile) {
      tiles += tile;
    } else {
    return isTile;
    }
    }

}

1 Answer

Your 'return isTile;' statement is inside the 'else' block. That means there is only a return value if the 'if' condition isn't verified. The compiler doesn't like that. You need to have a return statement in all cases.

Just put the return statement one curly brace lower, so it's outside of the else block. Alternately, it's also possible to just add a second return statement inside the if block, but in this case that would be unnecessary.

Thank you.

You're welcome!

Whenever an answer solved your issue in the Community, please consider selecting it as best answer. That way your question is displayed as resolved, and the person who answered you gets some points as a reward.