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 trialModure Rares
Courses Plus Student 9,041 PointsSyntax error...
I don't see where is my error. Please help me out...
public class ScrabblePlayer {
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) {
int number = 0;
for(letter : tiles.toCharArray()) {
if(hasTile(letter)) {
number +=1;
}
}
return number;
}
}
1 Answer
Steve Hunter
57,712 PointsHi Modure,
Your solution confuses the method parameter called letter
with a local variable in the for
loop called the same thing.
You want to create a local variable within the for
loop, compare that to the method argument/parameter, letter
, and increment a local count variable. Once the loop has finished, return the count variable.
Something like:
public int getCountOfLetter(char letter){
int count = 0;
for(char x : tiles.toCharArray()){
if(x == letter){
count++;
}
}
return count;
}
Steve.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsWhat's your specific error - did you click Preview?