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

Ara Brutian
Ara Brutian
565 Points

I don't know how to play scrabble so can't catch the logic of the excercise.

I dont understand what to apply the indexOf method to.

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

}

1 Answer

All you need to know is, the Scrabble player has tiles, and each tile is a character. Later the player can use the tiles to form words and earn points.

In the hasTile(char tile) method, you want to find out if the player already has a tile with that character or not. You're almost there.

tiles.indexOf(tile) will check the string 'tiles' and return the index of the character 'tile' in the string. If 'tiles' does not contain this character at all, the method returns -1. So you just apply indexOf to the 'tiles' string, and check if the result is -1.

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

Or this works just as well, using the getter instead of directly using the variable name. Don't forget the brackets, because it's a method.

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

This works in theory but is the wrong solution to the exercise because it adds unnecessary code. It does exactly the same thing than the above with more lines of code.

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

In your case, the "hasTile =" that you added in the brackets causes a compiler error. You don't need it! tiles.indexOf(tile) != - 1 is a boolean expression. It is either true or false. You can return it directly without the need to put it in a variable or to add an if block.

Guanhua Ding
Guanhua Ding
Courses Plus Student 2,679 Points

Thanks for your answer. I got it right, just not sure what the question actually wants. The description is a little bit confuse.