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

Lyndsi Barfield
Lyndsi Barfield
633 Points

I can not figure out what to do for this question. I'm honestly lost and don't where to start.

I attempted the code but I'm clearly not getting it right.

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
    if (isTile) {
     tiles +- tile; 
    } else {
      != tiles +- tile;
    }
    return false;
  }

}

1 Answer

This is a tough challenge. I had trouble with this one too because I was also trying to use an if/else statement. But the code challenge has an easier solution in mind.

In the video before the challenge Craig talks about using the indexOf method in order to return the index of a letter in the hangman answer.

indexOf('e') != -1;

In this case, the indexOf method is going to check the letter 'e' against the answer. If the letter 'e' is present then the index will be greater than -1.

For the hasTile method in the code challenge, you want to check against the tiles to see if the tile is present. If the tile's index is greater than -1, then the tile is present – and returns true.

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