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

Guy Hagan
Guy Hagan
1,985 Points

I can't complete this challenge

This is the task Can you also please help me write out the hasTile method? It should return true if the hand has the tile, and false if it doesn't. Thanks!

this the error Bummer! Hmmm, remember that the indexOf method on strings returns -1 if the char is missing, or greater than that if it is there. 0 is greater than -1.

Thanks all answers appreciated

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

    }
}

3 Answers

David Hamilton
David Hamilton
21,522 Points

Hello Guy,

The error you are getting says that indexOf returns -1 if the char is missing. So to get the challenge to pass use -1 instead of 0. I got it to pass with your code but with -1 instead of 0.

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)>-1;
       return hasTile;

    }
}

Code challenges are really picky.

Your code is actually correct.

The code challenge doesn't like you creating a variable and then returning the value of that variable, so you should try returning a value directly like this:

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

Good luck! ~alex

Guy Hagan
Guy Hagan
1,985 Points

I still get the same error message unfortunately but thanks anyway

Isaiah Marin
Isaiah Marin
11,971 Points

Hi Guy, you have the right idea.

The error message: "this the error Bummer! Hmmm, remember that the indexOf method on strings returns -1 if the char is missing, or greater than that if it is there. 0 is greater than -1."

The reasoning is because "mHand.indexOf(tile)" returns an integer related to the position of the character in the string.

So if you have the string, "hand" and you look for mHand.indexOf('a'). Then mHand.indexOf('a') would return 1.

Thus what you need to set is the following: "mHand.indexOf(tile) >= 0" since the tile your looking for can be in the position of 0 to (the length of mHand).

Here is the code to show exactly what I mean.

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 = mHand + tile;
    }

    public boolean hasTile(char tile) {
      if (mHand.indexOf(tile) >= 0)
      {
        return true;  // Return true if 0 or greater.
      }
      else
      {
        return false;  // Returns false if anything less than 0.
      }
    }
}