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 trialEmeka Okoro
Courses Plus Student 11,724 Pointsfor each loop
It says that the hand was "sreclhak", 'e' was checked , expected 1 but got 8. cant seem to find a solution
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 int getTileCount(char tile1) {
int count = 0;
for (char tile: mHand.toCharArray()) {
if (mHand.indexOf(tile1) >= 0) {
count++;
}
}
return count;
}
}
2 Answers
jcorum
71,830 PointsOne small error. You have this:
if (mHand.indexOf(tile1) >= 0) {
This checks to see if tile1 is in the hand. What you need instead is to see if the current tile equals the passed in one:
public int getTileCount(char tile1) {
int count = 0;
for (char tile: mHand.toCharArray()) {
if (tile == tile1) {
count++;
}
}
return count;
}
Emeka Okoro
Courses Plus Student 11,724 Pointsthanks . jst figured it out after running through the answers.
Dennis Addo
Courses Plus Student 2,943 PointsDennis Addo
Courses Plus Student 2,943 PointsInside the foreach loop you don't need indexOf again but you already have everything in charArray. if (tile == tile1) {
count++; }