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 trialMUZ140564 Kudakwashe Jena
4,895 PointsCan you also please help me write out the hasTile method? It should return true if the hand has the tile, and false if
where am i going wrong please help me.
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) {
return mHand.indexOf(tile) +=-1;
}
public boolean hasTile(char tile) {
boolean currentHand = mHand.indexOf(tile) >= 0;
if(currentHand){
return true;
} else {
return false;
}
}
3 Answers
Steve Hunter
57,712 PointsHiya,
You're pretty close to the answer here.
The key bit is:
mHand.indexOf(tile) >= 0;
The rest is just cleaning your code up a bit.
So, try this:
if(mHand.indexOf(tile) >= 0) {
return true;
} else {
return false;
}
That should work for you. It isn't massively different to what you wrote.
More simply, though:
public boolean hasTile(char tile) {
return mHand.indexOf(tile) >= 0;
}
The if
statement above adds little, if a little by way of clarity.
MUZ140564 Kudakwashe Jena
4,895 Pointspublic 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; // <-- I added this back in
}
public boolean hasTile(char tile) {
// boolean currentHand = mHand.indexOf(tile) >= 0; // <-- you don't need this line
if(mHand.indexOf(tile) >= 0) {
return true;
} else {
return false;
}
} // <-- closes the hasTile() method
} // <-- closes the class
/ScrabblePlayer.java:22: error: reached end of file while parsing } ^ 1 error thats the error am getting.So dont know now what to do.
Steve Hunter
57,712 PointsYou're missing two closing curly braces to close the method and then the class. I've added them in your code above, with comments.
Steve.