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

Kim Gee
Kim Gee
3,841 Points

code challenge String and char

Below is an object that I am using to represent a player in a Scrabble-like game I'm building. The mHand field is used to represent all the tiles the user currently has in their hand. Can you please fill out the addTile method so that it takes the char that is passed and adds it to the mHand member field? Thanks! so this is what I did and it's not working private String mHand; private String mAnswer; public void addTile(char tile) { // Adds the tile to the hand of the player boolean isHand = mAnswer.indexOf(tile) >= 0; return isHand; if(isHand) { mHand += tile; }}

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

    }

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

1 Answer

Your first part is quite straight forward

public void addTile(char tile) {
        // Adds the tile to the hand of the player
      mHand += tile;
    }. 

As for the second part

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

The only thing to remember here is indexOf method on strings returns -1 if the char is missing, or greater than that if it is there. 0 is greater than -1.

Hope that explains. Happy to help if you still have doubts.