Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Theodor Fahami
1,034 PointsNeed help with Treehouse challenge .indexOf (Java Programming)
I do not understand why my code is not working, my task is this: Can you also please help me write out the hasTile method? It should return true if the hand has the tile, and false if it doesn't. Thanks!
And here is the code i have writen:
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;
boolean hasTile = mHand.indexOf(tile) >= 0;
}
public boolean hasTile(char tile) {
return false;
}
}
Why does my code not work?
5 Answers

Blake Larson
12,990 PointsHey, You have the right idea, but he already set up the hasTile() method for you. Just take the mHand.indexOf(tile) >= 0 and wrap it in an if else statement in the hasTile() method. If the index is greater than 0 return true....else false.

Saiful Azhar
2,510 PointsI kinda understand that you want to test this out by simply returning false. I bet what you had in mind is that upon submitting the code, the system will simply evaluate the result. But the truth is, the "whatever" system they employ at the back not only check the return value, but also the syntax, naming, variable etc. In this case, they do expect to 'see' mHand.indexOf(tile) >= 0 instead of simply inspecting the result.
So, keep that in mind later on when you're working with other challenges. It doesn't behave exactly like your typical IDE or code editor. I believe treehouse should've told people about this in the first place.

Blake Larson
12,990 PointsLooks like he just didn't see the hasTile() method actually. Don't think he was trying to test it out returning false, and he had the right if statement, but it was in the addTiles() instead.

Saiful Azhar
2,510 Points@Blake Larson Ah.. I see.. Sorry I didn't notice that..

Blake Larson
12,990 PointsAll good. What you said is also true. The Treehouse tester does want it done a certain way.

Ninoslav Bozilovic
5,033 Pointspublic boolean hasTile(char tile) { boolean isTile = mHand.indexOf(tile) >= 0; if(isTile){ mHand += tile; } else { mHand += tile; } return isTile; }
I have the same problem i guess. He sands me eror like :Hmmm, remember that the indexOf method on strings returns -1 if the char is missing, or greater than that if it is there. 0 is greater than -1. How can I solve this problem. I think the logic is ok.?

Blake Larson
12,990 PointsThe question is asking if the passed in char "tile" is currently in mHand. There is no need to increment mHand.
So if you pass in 'f' and mHand if "first".
mHand.indexOf(tile); would be 0 and your method would return true because it's in mHand.

Ninoslav Bozilovic
5,033 Pointsok. I understand that. Increment was mistake. And now when I put mHand.indexOf(tile) >= 0 in if statement and in code block wright return true and else return false. compailer again send me eror. I'm sorry but I dont get it. :) Thank you for your answer Blake.

Ninoslav Bozilovic
5,033 PointsI got it finally. I rewright the code and everithing is fine now so I passed example.
Theodor Fahami
1,034 PointsTheodor Fahami
1,034 PointsThanks for the help! At first was a little confused of what meant but now I understand.