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

Rick Garland
Rick Garland
2,738 Points

cant convert string to boolean

got me again

ScrabblePlayer.java
public class ScrabblePlayer {
  // A String representing all of the tiles that this player has

  private String tiles;

   String ScrabblePlayer () {
      tiles = "";
     return 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
  // boolean tiles = tiles.indexOf(tiles) != -1;
    if (tiles) {
      tiles = tiles += tile;
      return true;
    }else {
    return false;
  }
}
}

2 Answers

michaelcodes
michaelcodes
5,604 Points

Hello there, for this one you were on the right track assigning the result to a boolean value. This method is only here to get whether or not the tile is contained within the tile String. Because of this we can simply return that Boolean value as so:

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

We can actually simplify this further by not even declaring a the boolean variable, and returning that directly:

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

Hope this helps!

Also, the reason you may have been getting the error talking about assigning an int to a boolean, the indexOf method returns an integer value, which represents its location within the String. In this case we don't really care where the letter is located, just that it is somewhere in there.

Manish Giri
Manish Giri
16,266 Points

For the hasTiles() method, you don't need to add tile to tiles like you're doing currently -

if (tiles) {
      tiles = tiles += tile;

What you should do is check if the char tile is present in the tiles string, using .indexOf(), and just return the result of the expression.

For example, if tiles is "matches", andtileisz- what willtiles.indexOf(tile)` return?