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 Storing Guesses

Andrico Karoulla
seal-mask
.a{fill-rule:evenodd;}techdegree
Andrico Karoulla
Front End Web Development Techdegree Student 13,760 Points

Trouble with part 2 of the Storing Guesses challenge

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) {
      if (mHand.indexOf(tile)) {
        return true;
      }
          else {
       return false;
          }
    }
}

I've got no clue where i'm going wrong... it says my if statement needs to return a boolean, but i've got no idea how to do that...

2 Answers

Stephen Bone
Stephen Bone
12,359 Points

Hi Andrico

The problem lies with the condition statement which is currently returning the position of the tile in mHand rather than returning true or false so the if statement knows which action to take.

You just need to add a test to see if the index is greater than or equal to 0 as this would indicate that the tile does exist in mHand.

So you should end up with code similar to below:

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

Hope it helps!

Stephen

Jeff Janes
Jeff Janes
8,033 Points

This worked for me, but is there a reason why we don't use "else" in this case?

Correct me if I'm wrong, but from my understanding. You don't need to add the 'else' code block in Stephen's code because 'return false;' is declared outside of the 'if statement' code block meaning that it will return false by default given that the specific tile that you're looking for can't be found or "indexed" inside of 'mHand'. I think that if you were to add the else portion of the conditional statement it would still work. It just isn't necessary.

I actually did my code a slightly different way if you're interested, and it still worked, I made sure to include what we just went over in the previous lesson to make sure I clearly understood the material:

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) {
      boolean hasTile = mHand.indexOf(tile) >= 0;
      if (hasTile) {
        return true;
      } else {
        return false;
      }
   }
}