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 trialVictor Yang
4,675 PointsJava ScrabblePlay For-Each Loop Task Issue
Hello,
I created a getTileCount() method that works when I tested it out in my Hangman Workspace. But the task is saying : "Did you forget to create the method getTileCount that accepts a char?" Can someone please clarify what the task is saying by "accepts a char"?
Does it mean that the method needs to be in 'getTileCount(char letter)' format (which can be unnecessary)?
Thank you!
Here is my code for the task:
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(){
int counter = 0;
for (char letter: mHand.toCharArray()) {
counter += 1;
}
return counter;
}
}
5 Answers
Christopher Augg
21,223 PointsHello Spencer,
Yes, you need to pass a character because the instructions say that you should only increment the count if the character passed into the method matches any of the characters in mHand as you are iterating through them. I hope that helps.
Regards,
Chris
Victor Yang
4,675 PointsHello Chris,
Thank you for the clarification. I thought the question was just asking for the number of tiles in a hand.
Jacob Arsement
3,048 PointsI'm having the same issue with this. Here is my code:
public int getTileCount(char letter) {
int count = 0;
for (char letter: mHand.toCharArray()) {
count += 1;
}
return count;
}
What is the alternative to traversing the characters in mHand instead of using for (char letter: mHand.toCharArray())
? I appreciate any help.
Christopher Augg
21,223 PointsThe for each loop is traversing each character of mHand. On the first iteration you get the first character of the array of characters within mHand and continue until the last character. The idea here is to test whether or not the character you pass into the method ever matches a character within mHand on any one iteration. Therefore, you should name the parameter that you are passing into the method with something different than letter (i.e. theLetter). Then use an if statement to test if letter == theLetter. Increment the count if it does. The reason you can use == to test instead of .equals stems from the fact that characters are essentially numbers via ASCII. I hope that helps.
Regards,
Chris
Rafael Cortes
1,174 PointsChristopher Augg I want to thank you for what you said here. I had done the challenge once before but I forgot how to solve it. Rather than look for the answer else where I was looking for a post that would help me think my way to the answer. Now I have it solved and learned something new about how to think like a programmer.
public int getTileCount(char tile) {
int counter = 0;
char theTile = tile;
for (char tiles : mHand.toCharArray()) {
if (tiles == theTile) {
counter++;
}
}
return counter;
}
Rafael Cortes
1,174 PointsI was just thinking about this solution that I came up with and I realized that I could have done without the
char theTile = tile;
I could have or should have just just passed the "char tile" into the "if" statement. But hey both ways work. I should written down the code on paper so that maybe I would have seen that I could have sharpen and cleaned up my code.
Tafadzwa Chiroro
8,188 Pointshaving the problem in solving this objective. hint me may be I will sail through.