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

as the code

thank you.my problem is from for loop.

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)
  {
    ScrabblePlayer player1 = new ScrabblePlayer();
    int count = 0;
    for (char player: tiles.toCharArray()){
    {
      if(hasTile.index0f(letter)!= 1)
      {
        count = player1.getCountOfLetter('p');
      }
    }
  }
  }
  }
Example.java
// 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');
  }
}

2 Answers

There are several problems with your getCountOfLetter method.

  • Why are you creating a new ScrabblePlayer object? You don't need your player1 variable at all. You're already in the ScrabblePlayer class. Everything your method does relates to the ScrabblePlayer object it belongs to. Don't create an extra one.
  • In your for loop, you named the char variable 'player'. But you're looping through each tile, not looping through each player. Just name your variable 'tile'
  • In your if condition, the way you use hasTile is wrong. The way hasTile works is, you pass it a character, and it returns true or false depending on whether that player has that character in his tiles. So you write hasTile(tile) and it returns true if the player has that tile. You can't use 'indexOf()' on it, and it's unnecessary. That code is already in the hasTile method.
  • Then you use 'count = player1.getCountOfLetter('p');' inside the getCountOfLetter method? When you do that, you tell java to go back at the beginning of the method, and do it all over again. As a rule, don't include a method inside itself. Or it will just call itself forever.

It looks a little like you were trying things randomly because you weren't sure what to do. You should start over calmly, going one step at a time. This is your task: 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.

  1. add a loop that goes through each of the tiles. Don't make it do anything yet.
  2. add an if block inside the loop. Inside the if brackets, check if the variable 'letter' is equal to the current tile
  3. now add the 'count' variable. Initialize it before the loop, like you do now. Then add 'count++' inside your if block to increase the count by one every time there is a tile identical to 'letter'
  4. after the loop block, return the 'count' variable

Yeah, Thank you!! what you said is all correct! I just write it more and more hurry and then find that I lose my patient then type it randomly, finally, I want to try to give an answer.

public int getCount0fLetter(char letter) { int count= 0; for(char tile: tiles.toCharArray()){ { if (letter == tile) { count ++; } } return count;

hope this could be right

Yeah, that's correct! We all try to go too fast sometimes, but learning to code is hard. Sometimes patience is needed :-)

Mihai Craciun
Mihai Craciun
13,520 Points

This is a way to solve the problem

public int getCountOfLetter(char letter){
    int counter = 0;
    for(char eachLetter: tiles.toCharArray()){
     if(eachLetter == letter)
       counter++;
    }
   return counter; 
  }

thank you