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 trialDax Stucki
1,034 PointsgetTileCount - not sure if I understand what is being asked of me
I've created a method for getTileCount, but I'm still not able to pass.
What am I missing?
public int getTileCount() { tileCount = 0; for (char letter: mHand.toCharArray()) { tileCount += 1; } System.out.println("Tile count is:" + tileCount); return tileCount; }
public class ScrabblePlayer {
private String mHand;
private int tileCount;
public ScrabblePlayer() {
mHand = "";
}
public String getHand() {
return mHand;
}
public int getTileCount() {
tileCount = 0;
for (char letter: mHand.toCharArray()) {
tileCount += 1;
}
System.out.println("Tile count is:" + tileCount);
return tileCount;
}
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;
}
}
2 Answers
Steve Hunter
57,712 PointsI had to read this challenge a couple of times. You don't want to count how many tiles the player has - there's no need to use a for:each
loop to do that. You can just use the length()
method for that! You're pretty close with your code but you need to pass a char
in, then iterate over the mHand
string.
The getTileCount
method needs to take a char
as a parameter. The for:each
loop then iterates through the mHand
string and counts how many instances of that char
occur. So, if the player's hand was H E L L O
, the method would be called like: getTileCount('L');
and it would return 2, the number of Ls in the hand. If the hand was T R E E H O U S E
, getTileCount('E');
would return 3, and so on.
My solution looks like:
public int getTileCount(char tile){
int count = 0;
for (char letter : mHand.toCharArray()) {
if(letter == tile){
count++;
}
}
return count;
}
I hope that makes sense!
Steve
Dax Stucki
1,034 PointsThank you !
This makes more sense. Much clearer.
Thanks very much.
Dax
Steve Hunter
57,712 PointsCool. Glad I helped.
Steve.