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

Daniels Ilori
Daniels Ilori
538 Points

Unreachable statement

@Steve Hunter "./ScrabblePlayer.java:26: error: unreachable statement return isPresent;" the above is the error message I am getting. What am I doing wrong?

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) {
    // TODO: Add the tile to tiles
    tiles += tile;
  }

  public boolean hasTile(char tile) {
    // TODO: Determine if user has the tile passed in
    boolean isPresent = tiles.indexOf(tile) >= 0;
    if (isPresent) { 
    return true;
    } else {
      return false;
    }
    return isPresent;
  }
}

2 Answers

Manish Giri
Manish Giri
16,266 Points

In this code -

 if (isPresent) { 
    return true;
    } else {
      return false;
    }

You have a return statement in both the if and else blocks. So depending on what the value of isPresent is - true or false, the method will return true or false accordingly, and terminate. It won't ever get to the final return statement - return isPresent;. Which is why you get that error.

Daniels Ilori
Daniels Ilori
538 Points

Ok, thanks! But I am still struggling with how to actually write that part properly.