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 trialNicole Favela
1,342 PointsI'm confused by the question. What am I doing wrong?
the instructions say to loop through the chars in the method and increment a counter if it matches. What is it?? Am I looking for a specific letter?
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()) {
if (tile == 'A') {
Count++;
}
return Count;
}
}
2 Answers
Damien Watson
27,419 PointsHi Nicole,
You are pretty close, the character you need to check against is the 'tile' argument which is passed in to the method. In your 'for' loop, you need to have a different variable name which then checks against what is passed in.
The only other thing is the 'return Count' should be outside of the 'for loop', otherwise it will only be called once and then return Count at what ever it got to.
public int getTileCount(char tile) {
int Count = 0;
for (char checkTile : mHand.toCharArray()) {
if (tile == checkTile) {
Count++;
}
}
return Count;
}
Nicole Favela
1,342 Pointsyeah, that example really helped simplify it. I appreciate it. that helped end my hour long headache. lol
Damien Watson
27,419 PointsCool, I love it when something finally makes sense. :)
Nicole Favela
1,342 PointsNicole Favela
1,342 PointsThanks! I didn't know you could add a new variable without declaring it beforehand. I still don't know what the question means when it says increment a counter if it matches. If it matches with what? What is checkTile checking for?
Damien Watson
27,419 PointsDamien Watson
27,419 PointsOk, I'll break it down. In the 'for each' loop, 'mHand' is being broken up into an array of characters which gets looped through, 'checkTile' is assigned the character in current focus.
So in the following situation, the 'for' loop would loop 5 times (one for each character).
Does this help explain it?