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 trialJef Davis
29,162 PointsgetTileCount Challenge
The error I am getting says something to the effect of: hand was sreclhak, tried 'e'. Expected 1 got 0. Within my for each loop, I'm using an if statement that, when equal to the char tile passed to it, will increment the counter. At least, that's what I'm hoping to do. Any ideas?
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) {
return mHand.indexOf(tile) > -1;
}
public int getTileCount(char tile) {
int tileCount = 0;
for (char count: mHand.toCharArray()) {
if (mHand.indexOf(count) == tile) {
tileCount++;
}
}
return tileCount;
}
}
2 Answers
Rebecca Bompiani
16,996 PointsHi Jefferson-
You're so close!
You've already established count as a charArray, passing into it the characters from mHand. The problem is, you're trying to find the letter of mHand using an index that is a letter you've passed into count.
Try replacing mHand.indexOf(count) with simply count
if(count==letter){...
the while loop will cycle through count as the characterArray sent from mHand. If it's the same as the letter the user has chosen, it'll add 1 to the count.
Jorge Flores
7,864 PointsHi jefferson, i think your mistake is on the condition, remember indexOf method returns the position of the char on the array, for example if you have: String name=Jefferson; name.indexOf('e');
the method will return 1 because its the postion of the letter on the array, wich means you are basically trying to compare an integer with a char.
I suggest you change the condition to check if the tile is equal to the letter on the array, using your code you will have a condition like this if(count==tile){ tileCount++; } You named each letter on the array as count, i also suggest you change the name to letter or something easier to understand, hope i was of some help.
Jef Davis
29,162 PointsThank you!
Jef Davis
29,162 PointsJef Davis
29,162 PointsYep. You're awesome. I knew my brain was just a little fried staring at this one. Thanks so much!