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

omkar chendwankar
omkar chendwankar
1,668 Points

Correct the existing hasTile method to return true if the tile is in the tiles field, and false if it isn't. You can sol

whats wrong in my code

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) {
    tiles+=tile;

  }

  public boolean hasTile(char tile) {
    boolean isTile=tiles.indexOf(tile);
    if(isTile>=0){
      return isTile;
    }
}

1 Answer

Cam Richardson
seal-mask
MOD
.a{fill-rule:evenodd;}techdegree
Cam Richardson
Front End Web Development Treehouse Moderator 16,895 Points

First off, it looks like your hasTile method is missing its closing brace. Secondly I get the following error:

./ScrabblePlayer.java:19: error: incompatible types: int cannot be converted to boolean
    boolean isTile=tiles.indexOf(tile);
                                ^
./ScrabblePlayer.java:20: error: bad operand types for binary operator '>='
    if(isTile>=0){
             ^
  first type:  boolean
  second type: int
2 errors

This is because you're trying to set a boolean to an integer value. Remember that the indexOf method will return -1 if an item is not found and >= 0 if it finds an item. So instead we can evaluate the boolean as such:

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

What this says is this: If the index of tile char in the tiles String is not equal to -1, it must be in the tiles String. It has the tile so we return true. Otherwise, we return false because -1 means that the tile char is not present in the tiles String.

Hope that helps!