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 trialyige yang
8,329 Pointswhy is "missing return statement" in my case
I print out the count. Why is my code wrong?
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) {
for (int count=0;tiles.indexOf(letter) != -1;count++) {
System.out.println(count);
}
}
}
1 Answer
Daniel Turato
Java Web Development Techdegree Graduate 30,124 PointsThat's not the correct way to complete this challenge. You need to loop over each individual character inside the tiles field, and compare it to the letter passed into the method. If they're equal, then you increase count then return the final count after the loop is completed. To loop over each individual character, you have to generate a char[] from tiles by using the toCharArray() method. So if you do all this, you should get this:
public int getCountOfLetter(char letter) {
int count = 0;
for(char c : this.tiles.toCharArray()) {
if(c == letter) {
count++;
}
}
return count;
}
yige yang
8,329 Pointsyige yang
8,329 PointsThank you so much. From your example I know how to use tocharArray( ) now.