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

jinhwa yoo
jinhwa yoo
10,042 Points

hasTile method??

"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!"

I don't really understand what the hasTile method is supposed to achieve exactly.

Is it that only one of each letter is allowed in the hand (not the case in Scrabble though I don't think)? Is it checking the number of tiles in the hand against a maximum amount?

Having problems writing the code because I don't understand what the hasTile method is actually for :-(

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

The instructions say the hasTile method should return true if the hand has the tile in it.

What we know:

  • function should be public
  • returns boolean (true/false)
  • accepts a char input

Needs

  • access hand
  • determine if tile (char) is present

The "hand" is in the String field mHand, and we learned that you can find out if a character is a member of a string using .indexOf(). If index is 0 or positive the tile (char) is present.

    // Task 2 of 2
    // 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!
    public boolean hasTile(char tile) {
       return mHand.indexOf(tile) >= 0;
    }