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

Mateusz Hyla
Mateusz Hyla
4,658 Points

What I do wrong in hasTile method ?

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

What I do wrong in this method. I got Bummer! but when I click preview there is even no error message, there is nothing ?

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 mHasTile = mHand.indexOf(tile) >= 0; 
        return mHasTile; 
    }

    public boolean hasTile(char tile) {
       return false;
    }
}

2 Answers

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hey,

You have two hastiles methods in your code. The stub was already there. You just needed to fill it in. The first thing to do is remove the last hastile method in your code.

The next thing to do is examine your code. Let's do it!

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

So, indexOf method. Only returns a 0 or a -1. 0 meaning it's there and -1 meaning it isn't there. So there is no need to do a >= to zero. Also, >= means that if the number is greater than or = to zero. If it were equal to zero that would mean it doesn't exist.

Now that we know indexOf is not going to return true or false, but a 0 or -1. We can do a conditional statement to determine what is going to be returned.

  1. First - let's get the return value of 0 or -1 and store it as an integer.
  2. Then, let's create an if condition that if true, returns false. Otherwise just return true.
 public boolean hasTile(char tile) {
      int a = mHand.indexOf(tile);
      if (a == -1) {
        return false;
      }
      return true;
}
}

Does that make more sense Please let me know if it doesn't and I would be happy to try and help you understand further if anything is unclear.

Thanks!

Mateusz Hyla
Mateusz Hyla
4,658 Points

Hey thanks a lot. I didn't notice somehow this duplicate method. I manage to accomplish that challange but I don't understand why in errors I didn't get any message what was wrong that I could fix that myself.

Thanks again ^_^.

Cheers