Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Creating the MVP Scrabble Tiles

Hello guys, I'm a bit lost on what to do here... I've tried several things, but I don't get it.

I can't figure out the second step. I've watched the video a couple of times, but still, I'm lost in this step.

Thank you in advance.

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
    return false;
  }

}

1 Answer

Vladut Astalos
Vladut Astalos
11,246 Points

You need to check if your tiles String contains that specific tile that is passed into the method. How do you do that? you check if the index of tile is a positive or a negative number. If it is positive that means that your string contains that specific tile if is negative that means that your string does not contains it. Your code should look like this:

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

That expression 'tiles.indexOf(tile) != -1' check if the index of tile in your tiles string does not equals -1 if it doesn't that means that string contains that tile so will return true, otherwise will return false.