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

index0f to check whether this character in this string or not .

I don't know what's wrong with my code here.Help someone give me an explaination,

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 has =tile.index0f(tiles)!= -1;
    if(has)
    {
      return true;
    }else{
    return false;
    }
  }

}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! First you have a small error in which you're checking for the index of tiles in tile, but you should be checking for the index of tile in tiles.

But above and beyond that, this particular challenge is looking for a very clean coding style. In general, it is considered bad practice to have an if/else statement that returns a true or false based on an evaluation being true or false.

Let me explain what I mean:

if(this == true) {
  return true;
} else {
   return false;
}

It would be much cleaner to return the evaluation directly:

return this == true;

If the evaluation is true, it returns true otherwise it returns false. So to apply this to the example at hand, you should be using:

return tiles.indexOf(tile) != -1;

If the index of the tile is not -1 (it exists), then return true, otherwise it returns false (doesn't exist).

Hope this helps! :sparkles: