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

Help, I'm stuck at Srabble tiles Task 2!

Correct the existing hasTile method to return true if the tile is in the tiles field, and false if it isn't. You can solve this a few ways, however, I'd like you to practice returning the result of the expression that is uses the index of a char in a String.

I tried that method that is written in the code but it says: Bummer! While you could definitely solve this using an if statement, try returning the result of the expression.

Thank you.

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.indexOf(tile) >= 0) {
    return true;
} else {
    return false;
}
  }

}
Craig Dennis
Craig Dennis
Treehouse Teacher

What's the expression? That's the part you've got in the if statement between the parenthesis.

6 Answers

Basically, you could do this:

return tiles.indexOf(tile) >= 0)

instead of

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

if this answers your question, please mark the question as answered.

Cristian Ilie
Cristian Ilie
3,289 Points

This worked, Thank you !

return tiles.indexOf(tile) >= 0;

Carlos Salcedo
Carlos Salcedo
1,788 Points

worked perfect, and had i had no idea how else to do it except for the if statement, thanks alot

Charlie Flowers
Charlie Flowers
612 Points

How do you do this I'm stuck on it?

ondrejhavazik
ondrejhavazik
1,577 Points

Can someone explain me the part, where I have to use ">= 0"?? .... Why simple tiles.indexOf(tile) don't work??

Craig Dennis
Craig Dennis
Treehouse Teacher

Hi ondrejhavazik !

indexOf will return -1 if the value is not found in the String.

Does that make sense now why it has to greater than or equal to 0 (zero is the first character)?

chase singhofen
chase singhofen
3,811 Points

i was lost with this one. i was using if statements.

with this workspace you must be careful not to add or delete any brackets. it doesnt work the same as an IDE. the syntax errors dont do much either it just points with the " ^ "

Raivis V
Raivis V
551 Points

return tiles.indexOf(tile) >= 0;

worked good for me,

Thank you

Thanks