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

Trouble with scrabble code quiz

Hey,

After finally figuring out what the question is actually asking, i'm ripping my hair out trying to figure out what i'm supposed to be doing. The error reporter is telling me that the random letter that is passed is returning true and incrementing my counter despite the fact it is not in the hand.

Please save me from losing my mind.

Thanks in advance Tom

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;
  private int counter;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }

  public int getTileCount(char givenLetter){
    for(char tile: mHand.toCharArray()){
      if(givenLetter == tile){
       counter++; 
      }      
    }
    return counter;
  }

  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;
  }
}

1 Answer

Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi Tom - You just forgot to declare and initialize counter:

 public int getTileCount(char givenLetter){
    int counter = 0;
    for(char tile: mHand.toCharArray()){
      if(givenLetter == tile){
       counter++; 
      }      
    }
    return counter;
  }

Thank you so much! Forgot to initialize. Problem solved :)

Kourosh Raeen
Kourosh Raeen
23,733 Points

Glad I could help! Happy coding!