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 trialRobert Roberson
Courses Plus Student 8,401 PointsTrying 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
28,558 PointsA 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.
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
Courses Plus Student 8,401 PointsThank 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
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.
Robert Roberson
Courses Plus Student 8,401 PointsRobert Roberson
Courses Plus Student 8,401 PointsThank you for a detailed explanation and the time that you put into it.