Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Creating the MVP Strings and Chars

Can you also please help me write out the hasTile method? It should return true if the hand has the tile, and false if

where am i going wrong please help me.

ScrabblePlayer.java
    public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }

  public void addTile(char tile) {
    // Adds the tile to the hand of the player
    mHand += tile;
  }

  public boolean hasTile(char tile) {
   return mHand.indexOf(tile) +=-1;
  }

  public boolean hasTile(char tile) {
      boolean currentHand = mHand.indexOf(tile) >= 0;
      if(currentHand){
        return true;
      } else {
        return false;
      }
    }

3 Answers

Hiya,

You're pretty close to the answer here.

The key bit is:

mHand.indexOf(tile) >= 0;

The rest is just cleaning your code up a bit.

So, try this:

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

That should work for you. It isn't massively different to what you wrote.

More simply, though:

public boolean hasTile(char tile) {
  return mHand.indexOf(tile) >= 0;
}

The if statement above adds little, if a little by way of clarity.

public class ScrabblePlayer {
    private String mHand;

    public ScrabblePlayer() {
        mHand = "";
    }

    public String getHand() {
       return mHand;
    }

    public void addTile(char tile) {
        // Adds the tile to the hand of the player
        mHand += tile;                    // <-- I added this back in
    }
public boolean hasTile(char tile) {
      // boolean currentHand = mHand.indexOf(tile) >= 0;  // <-- you don't need this line
      if(mHand.indexOf(tile) >= 0) {
           return true;
      } else {
           return false;
      }
   } // <-- closes the hasTile() method

} // <-- closes the class

/ScrabblePlayer.java:22: error: reached end of file while parsing } ^ 1 error thats the error am getting.So dont know now what to do.

You're missing two closing curly braces to close the method and then the class. I've added them in your code above, with comments.

Steve.