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

I'm stuck. Can someone please help me out?

Instructions are: "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!"

Can someone please walk me through 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 += tile;
    }

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

4 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Matthew;

In the hasTile() method we need to look at mHand and see if tile is in there, right? We should, therefore be able to use an if statement along the lines of:

if (mHand.indexOf(tile) >= 0) {
    return true;
} else {
    return false;
}

That will look through mHand, determine the index value of tile and if that index value is equal to or greater than 0, tile must, therefore, be in mHand, right?

We can also simplify our code a bit and clean it up by doing:

return mHand.indexOf(tile) >=0;

instead of that entire if... else statement.

Post back if you are still stuck.

Happy coding,
Ken

HI Ken,

This answer does not work. The answer looks correct but the code checker does not accept it. I filed an issue for this but no reply yet.

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Rahul;

I'm able to get that same code to function properly without error. Are you sure that the code is inserted in the correct method for Task 2?

Post back if you're still stuck.

Happy coding,
Ken

Hi Ken.

No. I am still not able to get it. Its task 2 and I am adding this in hasTile() method. I am getting following 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.

I added this : return mHand.indexOf(tile) >=0;

Thanks, Rahul

Ken Alger
Ken Alger
Treehouse Teacher

Can you post all the code from the code challenge for me to see?

Hi Ken, Here is the code :

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

}

Ken Alger
Ken Alger
Treehouse Teacher

Rahul;

Have another look at your addTile() method. That method doesn't currently work as desired. ;-)

Ken

Thanks Ken. I got it!

Ken Alger
Ken Alger
Treehouse Teacher

Rahul Daware -

Nice work and way to stick with it!

Happy coding,
Ken