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 trialKetan Parikh
2,222 PointsI am confused with this exercise
The question is loop through the hand and see if your tile matches the hand, then increment a counter.
I already have a method for hasTile and I am using it, so why is this not working?
Maybe the question is not clear to 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) >=0;
}
public int getTileCount(char tile) {
int counter = 0;
for(char ch : mHand.toCharArray()) {
if (hasTile(ch)) {
counter++;
}
}
return counter;
}
}
1 Answer
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsHey there Ketan, you're super close on this. the trouble is your if statement. What you are asking it to do right now is if the check and see if the value passed as ch matches matches a char in tile, which is either going to return 8, for true, since there are eight chars in the hand, or 0, if it doesn't.
What you'll want to do is check and see if ch is equal to tile, so something like this for your if statement.
if (ch == tile) {
counter++;
}
Should set you up nicely.
Thanks, let me know if this doesn't help and I'll try to clarify.
Ketan Parikh
2,222 PointsKetan Parikh
2,222 Pointsthanks that totally makes sense now. I am not sure why i kept checking hastile method instead.
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsRob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsNo worries, I've done multiple things like this myself, and once it occurs to me it's an "oh.. right, yeah that makes sense" type of moment.
Glad I could help.