Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Creating the MVP Storing Guesses

Robert Roberson
PLUS
Robert Roberson
Courses Plus Student 8,087 Points

Trying to figure this out! Can't get again after days of trying.

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 = mHand + tile;
}

public boolean hasTile(char tile) {
  boolean hasTile = hasTile += 1;
  if (hasTile)
         { return true;
         } else {
    return false;
    }

}

I couldn't get the page to click into "get help" this is the best I could do.

3 Answers

andren
andren
28,524 Points

A boolean variable can only contain the values true or false, it cannot contain a number, and a such you also cannot add a number to it. You are also not using the tile that get passed in to the method, which is the exact thing you are meant to compare the contents of mHand to.

In order to finish the hasTile method you need to add some evaluation that will be true if the tile is contained in the mHand variable and false if it is not, there are various ways of doing this but the simplest would probably be to use the indexOf method.

indexOf is a method that tells you what the index of a given char is in a string, if the char is not present in the string then it returns -1.

Knowing that we can write an expression like this:

 mHand.indexOf(tile) != -1

That statement will evaluate to true if indexOf returns any number other than -1, which means that the tile does exist in mHand, if it does return -1 then that will evaluate to false, and it will mean that the tile does not exist in mHand.

You could therefore use an if statement like this:

  if (mHand.indexOf(tile) != -1) {
        return true;
      } else {
        return false;
      }

That code would work, though it could also be shortened to this:

return mHand.indexOf(tile) != -1;

Since (mHand.indexOf(tile) != -1) evalutes to true if the tile is in mHand and false if it isn't, you don't actually need to use an if statement, you can just return the boolean value that the expression results in.

Robert Roberson
Robert Roberson
Courses Plus Student 8,087 Points

Thank you for a detailed explanation and the time that you put into it.

Matthew Francis
Matthew Francis
6,967 Points
 boolean hasTile = hasTile += 1;

You are adding a boolean and an integer together. This does not make sense.

Robert Roberson
Robert Roberson
Courses Plus Student 8,087 Points

Thank you for the explanation on how stupid I am. I already know that since I am learning to code and I don't know how to code.

Dan B
Dan B
6,155 Points

@andren Thank you so much for your explanation. I actually got put off Java for almost a week because I couldn't understand the logic in this code challenge. Now I think I get it perfectly.