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 trialSiddesh Gannu
3,565 PointsIm soo lost! Please help
I'm just not even sure what i'm doing wrong. Help please. I have the concept down but this just isn't making sense to me. Please help!
Thanks
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 tile) {
int count = 0;
for (char tile: mHand.toCharArray()) {
count++;
}
return count;
}
}
1 Answer
jcorum
71,830 PointsThe Preview shows this error:
./ScrabblePlayer.java:23: error: variable tile is already defined in method getTileCount(char)
for (char tile: mHand.toCharArray()) {
^
1 error
You use tile as the formal parameter and then again as the variable for the for-each loop.
The other problem is that you loop though the tiles in mHand, but you don't compare them to the formal parameter. Try this instead:
public int getTileCount(char tile) {
int count = 0;
for (char t: mHand.toCharArray()) {
if (t == tile) {
count++;
}
}
return count;
}
Siddesh Gannu
3,565 PointsSiddesh Gannu
3,565 PointsThanks