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 trialLucas Caldeira
977 Pointscould not solve this ,please help!
So back to that ScrabblePlayer. I found that it's not enough to know if they just have a tile of a specific character. We need to know how many they actually have. Can you please add a method called getTileCount that uses the for each loop you just learned to loop through the characters and increment a counter if it matches? Return the count, please, 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;
}
}
2 Answers
Christopher Augg
21,223 PointsHello Lucas,
We need to add a method called getTileCount(char tile) that returns an int and uses a for each loop to count how many characters match a tile character in the String mHand with a tile character that is passed into it. We can use the String method toCharArray() to provide the String in array form so that we can iterate over the chars. Craig gives a good example of how to do this in the video but you can also google how to loop over a String and find an example like:
http://stackoverflow.com/questions/3799130/how-to-iterate-through-a-string
Please let me know if you have any further issues and I can walk you through it in more detail.
Christopher Augg
21,223 PointsYou are very close. The indexOf(tile) >= 0 is your issue. You do not need this since you just want to compare the tile character passed in with the character from each iteration of the loop. Remember how Craig talked about a Char in the ASCII table was also a number? Well, you can do a simple if statement like if (letter == tile) { count++} because it will compare them. Just don't do that with Strings as you should always use the String.equals(String) for them. Hope this helps.
public int getTileCount(char tile) {
//increment a counter tells us we need a variable to hold an int called counter
int counter = 0;
//uses the for each loop. Craig went over this on the video and showed how
//to iterate over a String using the toCharArray() method.
for (char character : mHand.toCharArray()) {
// increment a counter if it matches. Hmm. Do we need to pass in a tile
// to test against each tile in mHand?
if (character == tile ) {
counter++; //increment counter - this must be an int
}
}
return counter;
}
Lucas Caldeira
977 PointsLucas Caldeira
977 Pointswhats wrong with this code ?? please help ! says !! 'e' was checked expected 1 but got 8.
public int getTileCount(char tile){ int count = 0; for (char letter : mHand.toCharArray()) { if (mHand.indexOf(tile) >= 0) { count += 1; } } return count;
}