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

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.