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

David Player
David Player
1,183 Points

ScrabblePlayer: I'm having an issue with this unreachable statement error.

I don't understand that I'm still having with this unreachable statement issue after I made an attempted to return a boolean value in a method.

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 answer = tiles.indexOf(tile) != -1;
    if (answer){
        return true; 
    } else{
      return false;
    }
    return answer; 
  }

}

3 Answers

andren
andren
28,558 Points

When you return a value the method stops running

If your if statement runs then your first return runs, and if it doesn't then the second return within the else statement is run. In what situation then will the third return ever run?

The answer is that there is no situation that could cause it to run. As Java would never reach it, as it is guaranteed to reach either the first or second return first. That's why Java complains that you have unreachable code.

You can fix that error by removing one of your return statements. But for this task you are actually required to return the Boolean expression directly, like you do in the third return. Using an if statement is not something the challenge allows.

So to pass just remove the if/else statement like this:

public boolean hasTile(char tile) {
    // TODO: Determine if user has the tile passed in
    boolean answer = tiles.indexOf(tile) != -1;
    return answer; 
}

You can also skip creating a variable and just return the expression directly like this:

 public boolean hasTile(char tile) {
    // TODO: Determine if user has the tile passed in
    return tiles.indexOf(tile) != -1;
}

Either way will work for this challenge.

Pranjal Agnihotri
Pranjal Agnihotri
4,187 Points
[Scrrible.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

  }

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

}
Hope it Helps.
Pranjal Agnihotri
Pranjal Agnihotri
4,187 Points

Hii according to your code why u r returning true and false just return answer as it has the value right or wrong

David Player
David Player
1,183 Points

Ah, I understand better now. Thank you for your big help!