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 (Retired) Creating the MVP Strings and Chars

Terrence Adderley
PLUS
Terrence Adderley
Courses Plus Student 775 Points

I am having some trouble with this. How do I add a tile to the users hand ?

I have set up the code for the "addTile" portion of code but I keep getting return messages stating that there is another more efficient method to use. In my head the code seems correct. I could use some fresh eyes.

ScrabblePlayer.java
public class ScrabblePlayer {
    private String mHand;

    public ScrabblePlayer() {
        mHand = "";
    }

    public String getHand() {
       return mHand;
    }


    public void addTile(char tile) {
         boolean gotTile = mHand.indexOf(tile) >= 0;
      if (gotTile) {
      mHand += tile;
      }
      // Adds the tile to the hand of the player

    }

  public boolean hasTile(char tile){

  return true;  
  }
}

2 Answers

Allan Clark
Allan Clark
10,810 Points
      boolean gotTile = mHand.indexOf(tile) >= 0;

This checks what the player already has in his hand for the given Tile.

if (gotTile) {
      mHand += tile;
      }

Now if the player has the tile in his hand, then we add the tile to his hand. It looks like the addTile method is like drawing a tile in scrabble, doesn't matter what the player has in his hand, he just adds it to his hand. So you may be able to cut all that down just to the mHand +=tile; concatenation.

What Allan Clark is saying is that boolean gotTile = mHand.indexOf(tile) >= 0; if (gotTile) { mHand += tile;} should be mHand += tile;.