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

Sahil Dhawan
Sahil Dhawan
1,536 Points

Not able to pass the second test case!

When a char which is not present in the string is passed as an argument in getTileCount it is returning 1 in place of 0.

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;
 int count = 0;

  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 int getTileCount(char tile)
  {
    for(char Tile : mHand.toCharArray())
    {
     if(Tile == tile)
      {
        count++; 
      }
    }
    return count;
  }
}

2 Answers

youre missing the count variable

public int getTileCount(char tile)
  {
    for(char Tile : mHand.toCharArray()) 
    {
     if(Tile == tile)
      {
        count++; 
      }
    }
    return count;
  }
```java
_________________________________________________________________________________________________
```java
// this works for me

 public int getTileCount(char tiles){
    int count = 0;
    for( char tile : mHand.toCharArray()){
      if(tile == tiles) count++;
    }
    return count;
  }
```java
Sahil Dhawan
Sahil Dhawan
1,536 Points

yes it worked fine . thanks for your help !

Sahil Dhawan
Sahil Dhawan
1,536 Points

count variable is declared in the beginning of the class so declaring it later should not make any difference !