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 trialSahil Dhawan
1,536 PointsNot 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.
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
metzu
5,907 Pointsyoure 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
1,536 Pointscount variable is declared in the beginning of the class so declaring it later should not make any difference !
Sahil Dhawan
1,536 PointsSahil Dhawan
1,536 Pointsyes it worked fine . thanks for your help !