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 trialRonit Mankad
12,166 PointsEverything is right but still getting error!
I'm Getting this error :" The hand was "sreclhak" and 'e' was checked. Expected 1 but got 8."
public class ScrabblePlayer {
private String mHand;
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 int getTileCount(char answer){
int count=0;
for(char letter :mHand.toCharArray()){
if(mHand.indexOf(answer)>=0){
count++;
}
}
return count;
}
public boolean hasTile(char tile) {
return mHand.indexOf(tile) > -1;
}
}
3 Answers
Jeremy Hill
29,567 PointsTry it like this:
public class ScrabblePlayer {
private String mHand;
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){
int count = 0;
for(int i = 0; i < mHand.length(); i++){
if(mHand.charAt(i) == tile)
count++;
}
return count;
}
}
anil rahman
7,786 Pointspublic int getTileCount(char tile){
int count=0;
for(char letter:mHand.toCharArray()){
if (letter == tile) {
count++;
}
}
return count;
}
Jeremy Hill
29,567 PointsWay to go :) I almost typed it out that way but it seems easier to interpret doing it the other way.
Ronit Mankad
12,166 PointsThanks I tried this format and it works too!
Jeremy Hill
29,567 PointsThat's good feed back. I haven't done the Java track yet, im actually doing web design. I learned Java in college so that's why my code isn't 100% with what you guys are being taught sorry.
anil rahman
7,786 PointsI find that the for loop is more difficult than a foreach loop in terms of understanding what's going on. It's much more compact i find and easier to read when using for each loops.
anil rahman
7,786 Pointsanil rahman
7,786 PointsIt says use the for each loop not for loop :)
Ronit Mankad
12,166 PointsRonit Mankad
12,166 PointsThank you very much it works!
anil rahman
7,786 Pointsanil rahman
7,786 PointsI also added the foreach loop way, which was how it should have been written for that specific question. :)