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

Jari Koopman
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jari Koopman
Python Web Development Techdegree Graduate 29,349 Points

strings and stars challenge 2

Hello there, I'm getting stuck with the second challenge. I thought it should be something with if and else. But don't know how, cause you have to compare a string with a char. I found an answer somewhere in the community, but couldn't work that one out and it didn't if and else like it was used in the video.

Can someone please help me out? Thank you!

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 (hasTile) {
       tile == mHand;
       return true;
     } else {
       return false;
    }
}

2 Answers

Gábor Tóth
Gábor Tóth
11,958 Points

Hi Jari!

Here's a possible solution:

public boolean hasTile(char tile) {
     if (mHand.contains(tile + "")) {  // check if mHand contains the char tile with the contains() method
       return true; // if yes return true and exit the method
     }
       return false; // if not, simply return false
}

I encourage you to look at this method more deeply and if you have questions, go and ask them!

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Jari,

Gabors advice is cool. You can use the indexOf method too. The code snippet is more clear then.

Like this:

public boolean hasTile(char tile) {
    return mHand.indexOf(tile) > -1;
// the indexOf method returns an integer
// if tile is not in mHand the integer will be -1 (returned boolean value is false)
// if tile is in mHand the integer will be greater then -1 (returned boolean value is true)
}

Grigorij