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

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: