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

Ahkeem Lang
Ahkeem Lang
16,358 Points

What in my indexOf method am I missing?!...

I also initialized and declared mMisses... Am I overthinking the issue here?

ScrabblePlayer.java
public class ScrabblePlayer {
    private String mHand;
    private String mMisses;

    public ScrabblePlayer() {
        mHand = "";
        mMisses = "";
    }

    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 inHand = mHand.indexOf(tile) >= 0;
      if (inHand) {
        mHand += tile;
      } else {
        mMisses += tile;
      }
       return false;
    }
}

2 Answers

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Hi Ahkeem,

Yes, you seem to have been overthinking this. The hasTile method only needs to return whether the char in the method's parameter exists in the mHand field. Here is the code that you can use:

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

Let me know if you need anymore help!

andren
andren
28,558 Points

You do indeed seem to be overthinking things slightly, and you seem to have slightly misunderstood what the method's purpose actually is, the method should return true if the tile passed to it is present in mHand and false if not, that is all the method should do. It should not add anything to any variable.

So there are two issues with your current code, firstly as I mentioned above your code does more than what the method is supposed to do, and secondly your code always returns false regardless of whether the tile is present in mHand or not.

All you need to do is change your code so that it returns true if the tile is in mHand and false if not, though it is also worth mentioning that there are multiple ways of achieving this, but the challenge is looking for one specific solution. More specifically it is looking for a solution that does not involve using an if statement like you are currently doing. So while this task can in theory be solved using an if statement that is not the most efficient way of solving it, and not a solution that the will be accepted as an answer to the challenge.

One hint I'll give is that this method can function with literally just 1 line of code, remember all you need to do is return some statement that will be true if the tile is in mHand and false if not, nothing beyond that is needed.

Ahkeem Lang
Ahkeem Lang
16,358 Points

Thank you so much!!