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 trialpratham daswani
979 Pointshow to get the tile count?
i seem to be counting the total number of characters , but here i need to match the character from the string and increment the counter depending on the number of times it occurs
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 tiles)
{
int tileCount = 0;
for (char tile: mHand.toCharArray())
{
if(mHand.indexOf(tiles)>=0)
{
tileCount++;
}
}
return tileCount;
}
}
1 Answer
Jeremy Hill
29,567 PointsThe argument passed in should only be one tile so you do not need mHand.indexOf() in there. Try:
if(tile == tiles)
tileCount++;
"tile" is your variable that is temporarily holding the char each time through the loop, so you want you use that to compare to the tile that is passed in as an argument. Also, the parameter "tiles" is a little misleading because only one tile will be passed in the method at any given time; so I would change it to "tile" and make your variable in your for loop something like "letter". The idea behind it is to make your program as clear as possible for other developers so they won't have to rely on comments and inquiries for understanding.
pratham daswani
979 Pointspratham daswani
979 Pointsthank you so much! precise.