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

Java Java Objects Creating the MVP Counting Scrabble Tiles

Counting how many of a certain char I have in a string

I understand that I need to increment countOfLetter every time that a char in String tiles matches the letter I search for, but I can't figure out how to put this into a method.

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 int getCountOfLetter (char letter) {
    int countOfLetter = 0;
    return countOfLetter;

  public boolean hasTile(char tile) {
    return tiles.indexOf(tile) != -1;
// This code is here for example purposes only
public class Example {

  public static void main(String[] args) {
    ScrabblePlayer player1 = new ScrabblePlayer();

    ScrabblePlayer player2 = new ScrabblePlayer();

    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');

2 Answers

Bob Allan
Bob Allan
10,900 Points

There is a LOT going on in this exercise, so I'll try to break it down into bite sized chunks.

You will need to loop through each letter in your String so that you can compare it to the letter in question. To do that you'll need to convert your tiles into a CharArray, so that you can loop through each letter and see if there are any matches. The variable that we will compare to each char in tiles we'll call foo (so that it is easy to tell apart).

Treehouse wants you to use an enhanced for loop to accomplish this task. It will basically say "For each char in the newly created tiles Char Array, do something".

Then create an if statement that says, "If foo matches the letter in question, increase countOfLetter by one."

Of course, you must declare your counter outside of your for loop, or else you can't return it.

Your format will look like this:

public int getCountOfLetter(char letter) {
   int countOfLetter = 0;
   return countOfLetter;

I hope this helps. :)

Thanks, that really helped!