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

Okay great, now can you fix the hasTile method for me, right now it always returns false.

Please help! If the tile is part of tiles return true, else false.

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

}

6 Answers

Hello

The if statement is fine. However, the instructor is just trying to show another approach that most experience programmers will use. Let add ( ) to the expression, for clarity.

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

this says ... return whatever inside the ( )

well, inside the ( ) is going to be evaluated to true or false

so, this one line will return true or false.

The if approach is doing same thing ... it just uses more space. Experience coders sometimes, they like to reduce number of lines they write. You may want to google "Java 8 Lambda Expression" This new feature in Java make writing codes even way less verbose

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

Thank you

Hello

Please note your error here:

if(tile == tiles) { ....

tile is a char where as titles is a String

I suspect you mean something like this:

if(tiles.indexOf(tile) != -1)

return true

otherwise

return false

If this answers your question, please mark the quattion as answered

Thanks

I'm confused.

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

That is the method that I made and it is asking me to return the expression.

Hello

the problem is not the method signature

you can't compare Char to String

Tile is a Char

Tiles is a String

two different data types.

You can also replace the entire body of the function with somethinf like this

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

Thanks

That return statement above is confusing. I feel that should be the if statement. I apologize if I'm confusing myself, I just want to not only pass the objectives, I want to understand what the objective is asking and what I am doing to solve it. Thank you for your patience.

I am getting this back: While you could definitely solve this using an if statement, try returning the result of the expression.

When I put in this method: public boolean hasTile(char tile) { // TODO: Determine if user has the tile passed in if(tiles.indexOf(tile) != -1) { return true; } else { return false; }

This challenge is a doozy!!!!!!

I solved it after looking at your answer again!!! Thank you so much!!!!!