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

Andrei Oprescu
Andrei Oprescu
9,547 Points

This question confuses me

I am currently given this question:

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.

And now I wrote this code:

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) { int countOfTiles = 0; countOfTiles = tiles.indexOf(letter); return countOfTiles; }

}

But surprisingly there is a bummer:

Bummer! Please use an enhanced for loop to cycle through each of the tiles found in tiles.toCharArray

Can you tell me what it is trying to say and what I should fix?

Thank you!

Andrei

ScrabblePlayer.java
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) {
  int countOfTiles = 0;
  countOfTiles = tiles.indexOf(letter);
  return countOfTiles;
 }

}

3 Answers

Try looping through each of the tiles. For each tile that matches the letter, increment countOfTiles.

public int getCountOfLetter(char letter) {
  int countOfTiles = 0;
  // for each tile in tiles
   // if letter matches tile
   // increment countOfTiles
  return countOfTiles;
Andrei Oprescu
Andrei Oprescu
9,547 Points

Thank you for your response.

Now I have this code but it still gives some errors:

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) { int countOfTiles = 0; for (int tiles : letter.toCharArray()) { if (tiles.IndexOf(letter) != -1) { countOfTiles++; } } return countOfTiles; }

}

Can you tell me what I did wrong?

This is what you currently have:

for (int tiles : letter.toCharArray()) {  // for each int tiles in letter
    if (tiles.IndexOf(letter) != -1) { // if char letter is found in int tiles
        countOfTiles++; // increment countOfTiles
    }
}

What you should have:

// for each char tile in tiles (remember tiles is already a String)
   // if letter = tile
      // increment countOfTiles

Hope this helps!