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 trialnandup
5,183 PointsI'm unable to understand how to solve this problem.
You'll need to use your skills to loop through each of the tiles, use an equality check, and then increment a counter if the tile and letter match. You got this! Now in your new method, have it return a number representing the count of tiles that match the letter that was passed in to the method. Make sure to check Example.java for some example uses.
public class ScrabblePlayer {
// A String representing all of the tiles that this player has
private String tiles;
public ScrabblePlayer() {
tiles = "";
}
public String getTiles() {
return tiles;
}
public void addTile(char tile) {
tiles += tile;
}
public boolean hasTile(char tile) {
return tiles.indexOf(tile) != -1;
}
public int getCountOfLetter(char letter){
}
}
1 Answer
Umesh Ravji
42,386 PointsHi Narender, one way to handle this would be to determine how many times the tile
appears in the tiles
string by converting it to an array
of char
, and using a for
loop to increment over it, keeping a count of each time that the tile
is equal to the letter
given to the method.
public int getCountOfLetter(char letter) {
int count = 0;
for (char tile : tiles.toCharArray()) {
// increment count if letter is the same as the tile, your task :)
}
return count;
}
nandup
5,183 Pointsnandup
5,183 PointsThank you very much man!!