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 trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Bianca Christian](https://secure.gravatar.com/avatar/728887e15a94b9a70b5c8d78b3e54b72?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Bianca Christian
3,524 PointsNot sure how to loop through the array and increment counter if letter entered through method's parameter is in the arr
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 getTileCount(char letter){
int count = 0;
for (char letters : tiles.toCharArray()) {
if (letters == letter){
//counter ++;
count = letters.getCountOfLetter(letter);
}
}
return count;
}
}
// This code is here for example purposes only
public class Example {
public static void main(String[] args) {
ScrabblePlayer player1 = new ScrabblePlayer();
player1.addTile('d');
player1.addTile('d');
player1.addTile('p');
player1.addTile('e');
player1.addTile('l');
player1.addTile('u');
ScrabblePlayer player2 = new ScrabblePlayer();
player2.addTile('z');
player2.addTile('z');
player2.addTile('y');
player2.addTile('f');
player2.addTile('u');
player2.addTile('z');
int count = 0;
// This would set count to 1 because player1 has 1 'p' tile in her collection of tiles
count = player1.getCountOfLetter('p');
// This would set count to 2 because player1 has 2 'd'' tiles in her collection of tiles
count = player1.getCountOfLetter('d');
// This would set 0, because there isn't an 'a' tile in player1's tiles
count = player1.getCountOfLetter('a');
// This will return 3 because player2 has 3 'z' tiles in his collection of tiles
count = player2.getCountOfLetter('z');
// This will return 1 because player2 has 1 'f' tiles in his collection of tiles
count = player2.getCountOfLetter('f');
}
}
1 Answer
![Dane Parchment](https://uploads.teamtreehouse.com/production/profile-photos/352832/micro_Dane_s_Profile_Photo_Cropped.jpg)
Dane Parchment
Treehouse Moderator 11,076 PointsAlright so let me walk you through this:
At this point in the challenge we should have already created the method to get the count of the letters:
public int getCountOfLetter(char letter) {
return 0;
}
So now let's actually make it return a count instead of a fixed number. We will do this by creating a variable to represent this count, and start it off at the value 0 (we assume by default that there are no letters that match the one we are looking for).
public int getCountOfLetter(char letter) {
int counter = 0;
return counter;
}
Now this does the exact same thing we did previously but now we have synced the return value to that of a variable, so the value can be changed and isn't fixed anymore. So now that, we finished that, let's loop through the tiles String. To accomplish this we will be using an enhanced for-loop, which is something you should have learned by now.
public int getCountOfLetter(char letter) {
int counter = 0;
for(char c : tiles) {
}
return counter;
}
The problem with this implementation is that tiles is a the char c and String tiles are not compatible in an enhanced for-loop, we need to convert tiles into an array of chars. Luckily there is a method available to do just that, and you should have learned it by now:
public int getCountOfLetter(char letter) {
int counter = 0;
for(char c : tiles.toCharArray()) {
}
return counter;
}
Now that we are iterating through the String, we need to compare whether or not the current letter in String we are looping through matches the letter we are getting a count of, we can do this via a conditional statement:
public int getCountOfLetter(char letter) {
int counter = 0;
for(char c : tiles.toCharArray()) {
if(c == letter) {
}
}
return counter;
}
Great! But now we need to change the count, correct? Well since found the letter, all we would need to do is increment the counter by 1, and then we are good because once we exit the loop we will have the total count of matching letters.
public int getCountOfLetter(char letter) {
int counter = 0;
for(char c : tiles.toCharArray()) {
if(c == letter) {
counter++;
}
}
return counter;
}
That's it we are done!
If you have any questions feel free to ask me.