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 trialGuy Hagan
1,985 PointsI can't complete this challenge
This is the task Can you also please help me write out the hasTile method? It should return true if the hand has the tile, and false if it doesn't. Thanks!
this the error Bummer! Hmmm, remember that the indexOf method on strings returns -1 if the char is missing, or greater than that if it is there. 0 is greater than -1.
Thanks all answers appreciated
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) {
boolean hasTile= mHand.indexOf(tile)>0;
return hasTile;
}
}
3 Answers
David Hamilton
21,522 PointsHello Guy,
The error you are getting says that indexOf returns -1 if the char is missing. So to get the challenge to pass use -1 instead of 0. I got it to pass with your code but with -1 instead of 0.
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) {
boolean hasTile= mHand.indexOf(tile)>-1;
return hasTile;
}
}
Alexander Davison
65,469 PointsCode challenges are really picky.
Your code is actually correct.
The code challenge doesn't like you creating a variable and then returning the value of that variable, so you should try returning a value directly like this:
public boolean hasTile(char tile) {
return mHand.indexOf(tile) > 0;
}
Good luck! ~alex
Guy Hagan
1,985 PointsI still get the same error message unfortunately but thanks anyway
Isaiah Marin
11,971 PointsHi Guy, you have the right idea.
The error message: "this the error Bummer! Hmmm, remember that the indexOf method on strings returns -1 if the char is missing, or greater than that if it is there. 0 is greater than -1."
The reasoning is because "mHand.indexOf(tile)" returns an integer related to the position of the character in the string.
So if you have the string, "hand" and you look for mHand.indexOf('a'). Then mHand.indexOf('a') would return 1.
Thus what you need to set is the following: "mHand.indexOf(tile) >= 0" since the tile your looking for can be in the position of 0 to (the length of mHand).
Here is the code to show exactly what I mean.
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) {
if (mHand.indexOf(tile) >= 0)
{
return true; // Return true if 0 or greater.
}
else
{
return false; // Returns false if anything less than 0.
}
}
}