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 (Retired) Creating the MVP For Each Loop

Matthew Fueglein
Matthew Fueglein
640 Points

cannot find symbol

i'm getting this error, also not sure if i understand whats being asked. Am I supposed to make a variable called count in the method and add to it if the tile matches something in the hand?

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }

  public void addTile(char tile) {
    // Adds the tile to the hand of the player
    mHand += tile;
  }

  public boolean hasTile(char tile) {
   return mHand.indexOf(tile) > -1;
  }
 public String getTileCount() {
    for (char tile: mHand.toCharArray()){
     int count = 0;
      if(mHand.indexOf(tile)>= 0){
        count ++;

      }
    }
  return count;
  }
}

1 Answer

Rebecca Rich
PLUS
Rebecca Rich
Courses Plus Student 8,592 Points

The idea of this method is to count the number of times a character shows up in the mHand string; i.e. how many of a certain tile you have in your scrabble hand. You will want an input argument for this method to represent the character (or tile letter) that you are trying to count. So, you will want something like getTileCount(char tileSearchingFor){...}.

Also, we are trying to get a tile count. Counts are usually represented by an integer number, e.g. 1, 2, 3, etc. So for your method statement, something like public int getTileCount(char tileSearchingFor) would be better. It also matches the variable type that you are currently trying to return in the method.

That being said, the count variable should be initialized to zero outside of the for loop, otherwise it will just get reset to zero every time you run the for loop, and you will have an error when trying to return count, as it will not be in scope at that point in the method.

Next, the for loop is written so that it will walk through all of the characters in the mHand string, each time the current character will be represented by the variable tile in the for loop. Therefore, we only want to increment the count when that character (or tile) is the same as the tile that we are looking for (or ```tileSearchingFor).

So something like:

public int getTileCount(char tileSearchingFor) {

    int count = 0; // initialize count variable to zero

    // walk through each character in the mHand String.
    for (char tile: mHand.toCharArray()){
          // if the tile is the same as the tileSearchingFor, we want to increment the count.
          if(tile == tileSearchingFor){
             count++;
          }
    }

    // once we have walked through all of the characters, return the count for the number
    // of times we have seen the tile we are searching for.
    return count;
}

Let us know if you have any more questions.