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

Yahya Saadi
Yahya Saadi
4,277 Points

Boolean

The code should read if it the hand has a tile. In the last block of code. What should I write inside the if().

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) {
      if () {
        return true;
      } else {
        return false;
      }

    }
}

2 Answers

rydavim
rydavim
18,813 Points

You'll want to check the String mHand to see if the char tile is in it. Consider using something like indexOf() to evaluate this. Remember that if the character is in the string, indexOf() return the index value. If it is not in the string, it returns a -1.

Here's a syntax example to get you started...

string.indexOf(character)
deckey
deckey
14,630 Points

Hi Yahya, to check if the tile is in the mHand string, use 'indexOf()' method, as used in the videos. This method is useful since it returns -1 if no matched value found.

try it and good luck!