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 trialHank Tran
652 PointsWhat should I do?
Help me,please!
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;
}
//I have a problem here
public int getTileCount(char name) {
int counter = 0;
for (char letter : ) {
counter++;
}
return counter;
}
}
1 Answer
Alex Wall
4,086 PointsI can't tell from the question exactly which exercise this was, but I'll take a stab at the one that I think it might be. I think the assignment was to make a method that will run through the hand and tell you how many items were in the hand. If so, you create this method that returns an integer, you create a counter integer and set it to zero, then you use the for loop to iterate through the characters of the hand. We use the toCharArray() method to turn the hand into an array of characters. Each time a letter equals a tile, we increment the counter by one, finally returning the counter.
public int getTileCount(char tile) {
int counter = 0;
for (char letter: mHand.toCharArray()) {
if(letter == tile) {
counter += 1;
}
}
return counter;
}
Hank Tran
652 PointsHank Tran
652 PointsThanks Alex Wall! It works