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 trialOrit Lieber
11,181 PointsWhat is wrong with this boolean return type method?
I was asked to fill in the hasTile method so it return true if the mHand String has the tile char init and false if it doesnt I cant seem to figure out why it's not compiling please help. the code is attached. thanks
public class ScrabblePlayer {
private String mHand;
public ScrabblePlayer() {
mHand = "";
}
public String getHand() {
return mHand;
}
public void addTile(char tile) {
mHand += tile;
}
public boolean hasTile(char tile) {
if (mHand.indexOf(tile)>=0){
hasTile=true;}
return hasTile;
}
}
1 Answer
Jennifer Nordell
Treehouse TeacherhasTile is already defined as a function. Later you use hasTile as a variable to hold a boolean value. Either you need to use a variable besides hasTile (so that it's different from the function name) or you need to not use it at all and straight out return the boolean values. Take a look at how I did it:
public boolean hasTile(char tile) {
if (mHand.indexOf(tile) >= 0) {
return true;
} else {
return false;
}
}
Orit Lieber
11,181 PointsOrit Lieber
11,181 PointsThanks Jennifer! I used the first option and it worked :-) Thanks for helping me with this blocker.... :-)
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherYou're quite welcome! :)
Isam Al-Abbasi
2,058 PointsIsam Al-Abbasi
2,058 PointsHello Jennifer Cordell I tried to use the code you wrote but still not working!!
take a look please:
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherIsam Al-Abbasi Hi! Your code isn't like mine. You've replaced one of my curly braces with a semicolon. Here's the line (according to what you posted)
if (mHand.indexOf(tile) >= 0);
It should be
if (mHand.indexOf(tile) >= 0) {