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 trialSimon Johnson
1,277 PointsCannot complete the coding challenge
I have been at this for about 30 minutes now. I have looked at the forum, I have googled to try and figure it out, what am I doing wrong?
I thought I was getting it and understood it, and then this challenge came along and threw me.
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 String getTileCount(char tile) {
int counter = 0;
for (char letter : mHand.toCharArray()) {
if(mHand.indexOf(tile) > -1) {
++counter;
}
}
return String.valueOf(counter);
}
}
I changed the code block to the following..
public boolean hasTile(char tile) {
return mHand.indexOf(tile) > -1;
}
public int getTileCount(char tile) {
int counter = 0;
for (char letters : mHand.toCharArray()) {
if(mHand.indexOf(tile) > -1) {
counter += 1;
}
}
return counter;
}
but now I get the error 'Expected 1, got 8'
2 Answers
Simon Johnson
1,277 PointsI found the answer in the forum, in another thread (sorry for double posting. I have to say the wording of the challenge was far too vague this early on in the course. Maybe some reminders/hints (I know we won't get them in the big bad world of real life, but at this stage??).
Answer I used was as below
public int getTileCount(char tile) {
int count = 0;
for (char letter : mHand.toCharArray()) {
if (tile == letter) {
count++;
}
}
return count;
}
but I still dont quite understand the use of the == ( i understand the function of the ==, just not the use in this context, as the previous video had no reference to anything like that and surely we were checking knowledge of that particular video?
Especially as in other forum posts this line is referenced as part of the answer...
if(mHand.indexOf(tile) > -1)
Jeremy Hill
29,567 Pointsthe "==" means "is equal to", this is checking a condition; when it is just one '=' this is setting the value.
Jeremy Hill
29,567 Pointspublic int getTileCount(char tile){
int count = 0;
for( char letter : mHand.toCharArray()){
if(letter == tile)
count++;
}
return count;
}
Simon Johnson
1,277 PointsSimon Johnson
1,277 PointsHi Jeremy
Yes I had noted the double equals (==) was to denote a does this equal that, rather than a single equals (=) to assign a value to a variable, but I was more questioning the use of it in the answer, when it hasnt been referenced for such a time int eh video series, and is not implied anywhere in the challenged.
Thanks for the assistance with the answer though, much appreciated.