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

Furkan Celik
Furkan Celik
646 Points

stuck in Scrabble task

i really didn't understand what should i do

ScrabblePlayer.java
public class ScrabblePlayer {
  // A String representing all of the tiles that this player has
  private String tiles;
  private String passed;
  private String missed;

  public ScrabblePlayer() {
    tiles = "";
  }

  public String getTiles() {
    return tiles;
  }

  public void addTile(char tile) {
    // TODO: Add the tile to tiles
    boolean isUsed = tiles.indexOf(tile) != -1;
    if (isUsed) {
      passed += tile;
    } else {
      missed += tile;
    }
    return isUsed;
  }

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

}
Furkan Celik
Furkan Celik
646 Points

I'm really appreciate with your help thank you very much

2 Answers

Cameron Hastings
Cameron Hastings
3,439 Points

You almost have it. You just put your code in the wrong method.

Okay, so here is the first task:

"For this first task, modify the addTile method so that it appends the tile that was passed in, to the player's tiles. Practice using the += shortcut method for string concatenation."

It wants you to add the char tile to the String tiles, which is stated above in the fields.

You can do it like this:

ScrabblePlayer.java
 public void addTile(char tile) {
    tiles = tiles + tile
  }

BUT it wants you to "Practice using the += shortcut method for string concatenation." So you should rewrite it like this:

Solution to Task 1:

ScrabblePlayer.java
 public void addTile(char tile) {
    tiles += tile;
  }

Here is the second task:

"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 uses the index of a char in a String."

Like what was done in your code and in the video, you need to set up a boolean in the hasTile method (not the addTile method). Like this:

ScrabblePlayer.java
public boolean hasTile(char tile) {
    boolean isUsed = tiles.indexOf(tile) != -1;
    return False;
}

.indexOf() will tell you the location of a char within a String by providing a numbered location (0 to whatever). If it is not present, it will return '-1'. This boolean says 'set isUsed to true, if tiles.indexOf(tile) is not equal to -1'.

From there, all you have to do is change the return false' to 'return isUsed'.

Solution to Task 2:

ScrabblePlayer.java
public boolean hasTile(char tile) {
    boolean isUsed = tiles.indexOf(tile) != -1;
    return isUsed;
  }

That's it. Hope this helped.

Esmerald Seitlli
Esmerald Seitlli
3,587 Points

public void addTile(char tile) { tiles += tile; } is not working