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 trialTaebin You
4,786 PointsSo back to that ScrabblePlayer. I found that it's not enough to know if they just have a tile of a specific character
Why wouldn't this work?
I cannot understand...
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 String getTileCount() {
int counter = 0;
for (char tile: mHand.toCharArray()) {
if(mHand.indexOf(tile) >= 0){
counter++;
}
}
return Integer.toString(counter);
}
}
1 Answer
Jennifer Nordell
Treehouse TeacherHi there! You're actually fairly close here. But your method should be accepting a char and returning an integer. Your method accepts nothing and returns a string. I posted a fairly detailed explanation and solution here quite a while ago. Take a look and see if it helps clarify things! Good luck!
Taebin You
4,786 PointsTaebin You
4,786 PointsIs there any way that I can use .indexOf instead of if ==? I am trying to use indexOf with your answer, but can't figure it out. I am not quite sure how I how I should use it
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherNot that I'm aware of. Keep in mind you're looking at two tiles and see if they match. You're looking at that tile in that spot of your hand and comparing it to the tile you're search for. Take a look again at my solution linked above.