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

hasTile() method

boolean intile=tiles.indexOf(tile)!=-1; if(intile) { return true;

}
  else
  {
return false;


  }
return intile;

}

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
    boolean intile=tiles.indexOf(tile)!=-1;
    if(intile)
    {
      return true;



    }
      else
      {
    return false;


      }
    return intile;

  }


}

3 Answers

I'm not suggesting you use a boolean intile at all.

The tiles.indexOf(tile) expression will return the index where tile occurs within the tiles character array. This will be between zero, the first element of the character array, and the length of the array (minus 1).

So, if tiles.indexOf(tile) returns a figure that is greater than or equal to zero, this means that tile is contained within tiles, right? And that's what we're trying to find out. So, the result of tiles.indexOf(tile) >= 0; resolves to true if tile is within tiles or false if tile is not within tiles.

Therefore, returning the result of that expression supports the sole purpose, or single responsibility, of the hasTile() method. The method looks like:

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

So, I was never suggesting that you check for equality to zero, with == 0, although the expression can return zero, for the above reasons, I hope you realise why the solution suggested here does make sense.

Else, let me know why and I will try to explain further.

Steve.

Hi there,

You need to test the indexOf, as you have done - you've got the tiles.indexOf(tile) part correct.

Now, compare using >= whether the result of tiles.indexOf(tile) is zero or greater. Just return the result of that comparison. You don't need an if statement - the comparison gives you the result you need; adding an if statement just clutters the code. Like:

if(true or false expression){
  return true;
} else {
  return false;
}

That above is the same as:

return true or false expression;

Make sense?

Steve.

no my friend, it makes no sense coze the condition we are checking here never returns zero so we can't check boolean intile=tiles.indexOf(tile)==0, this returns true if condition is met and -1 if tile is not in tiles.

thanks.