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

Angelos Kalandranis
Angelos Kalandranis
661 Points

How do I return the result of the expression?

In the second challenge task of scrabble.

ScrabblePlayer.java
public class ScrabblePlayer {
  // A String representing all of the tiles that this player has
  private String tiles;
  private String hasTile;
  public ScrabblePlayer() {
    tiles = "";
  }

  public String getTiles() {
    return tiles;
  }

  public void addTile(char tile) {
    // TODO: Add tthe tile to tiles
    tiles += tile;

  }

  public boolean hasTile(char tile) {  
    boolean hasTile = tiles.indexOf(tile) != -1;
    if(hasTile){
    return hasTile;}
    else{
    return false;
    }
  }
}

1 Answer

Ronald Williams
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ronald Williams
Java Web Development Techdegree Graduate 25,021 Points

You can simply return the hasTile boolean that you make. That boolean is going to either return true or false. You do not need to use an if else statement.

Craig Dennis uses an if else statement in the video because his applyGuess method has an additional step of adding the letter to isHit or misses.

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