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

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

I don't understand at all :(

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
    if (tiles += tile) {
      return true;
    } else {
      return false;
    }

  }

}

5 Answers

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

Hi there, cesar! I received your request for assistance. You did the first part just fine, but I'm concerned that you may be misunderstanding what the challenge is asking. We want the program to take a group of letters and ask if the player has one of them in their hand. As hinted at in the challenge instructions, you will need to use the indexOf method to do this. You will be looking for the index of the tile inside of the tiles string. If the tile is not found, the value returned by the indexOf method will be -1 indicating that the tile was not found. So you can set up a comparison to check if the result of the indexOf method was greater than or equal to 0. If it was, then the tile was found, otherwise it was not.

Note that while you can do this with if/else statements, this approach will not be accepted by the challenge. This should all be done in one line of code inside a return statement.

I hope this helps, but let me know if you're still stuck! :sparkles:

How am I going to solve this without using if/else statement? I am stucked for 1 hours now. Please help me.

Bhawani Shankar
Bhawani Shankar
954 Points

public boolean hasTile(char tile) { // TODO: Determine if user has the tile passed in return tiles.indexof(tile)>0; }

Gonzalo Torres del Fierro
PLUS
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

return tiles.indexof(tile)>0;

this expression it is ok and

return tiles.indexof(tile)>=0;

must work as well...

Albert González
Albert González
22,953 Points

It's "return tiles.indexOf(tile)>0;".

Be careful because Java it's sensitive with the methods.

why this is not working?

public boolean hasTile(char tile) { // TODO: Determine if user has the tile passed in

if(tiles.indexOf(tile)>0){
  return  true;
}
return false;

}

}