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

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

challenge 2/2 scrabble

i think my code is right, but keep telling me that im worng... can not find the problem..

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

2 Answers

andren
andren
28,558 Points

You solution is not technically wrong, but it's worth taking a look at the actual error message your code produces, which is this:

Bummer! While you could definitely solve this using an if statement, try returning the result of the expression.

There are many ways of solving this challenge, and while using an if statement works it's not the most efficient way to do it, this challenge is setup to tech you how to do things a bit more efficiently.

One hint I'll give you is that this method can function with literally just one line of code. The expression that you assign to the isHit variable is true if the tile is in the tiles field and false if it is not, and that is also exactly what the method is supposed to return when it is run. So try to think of ways that you could return that result that does not involve using an if statement. Remember that any value that can be assigned to a variable can also be returned directly using the return keyword, you don't have to assign something to a variable if you only use it in one place.

If you still can't think of the solution, just ask me again and I'll post the solution for you.

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

public boolean hasTile(char tile) { // TODO: Determine if user has the tile passed in return tiles.indexOf(tile) !=-1; }

// a bit more efficiently.

Josh Vaisanen
Josh Vaisanen
680 Points

Thanks for the answer, I understand now.