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

M G
M G
2,833 Points

Stuck on Challenge Task 2 Stage 3 Java Objects

Having issues with method "hasTile", I don't know what I am doing wrong, can somebody please help me with this.

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 = Character.toString(tile);
    }

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

2 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Your addTile method is wrong. It needs to append. I will fix that, whoops!

Your second method, hasTile looks good. You could also just return the expression as it returns true or false.

M G
M G
2,833 Points

I got it and fix it. Thank you!

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

I haven't done this one before but it seems to me like your hasTile method is not actually returning anything we can fix this one of two ways:

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

Or......

public boolean hasTile(char tile) {
      return mHand.indexOf(tile) >= 0;
}
M G
M G
2,833 Points

Thank you, I appreciate it.