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 trialJack Cummins
17,417 PointsI need help with task 2. I give out best answers.
Correct the existing hasTile method to return true if the tile is in the tiles field, and false if it isn't. You can solve this a few ways, however, I'd like you to practice returning the result of the expression that uses the index of a char in a String.
public class ScrabblePlayer {
// A String representing all of the tiles that this player has
private String tiles;
private String answer;
public ScrabblePlayer() {
tiles = "";
}
public String getTiles() {
return tiles;
}
public void addTile(char tile) {
// TODO: Add the tile to tiles
tiles += tile;
}
public boolean hasTile(char tile) {
// TODO: Determine if user has the tile passed in
boolean hh = answer.indexOf(tile) != -1;
}
if (hh){
}
}
3 Answers
Steve Hunter
57,712 PointsHi Jack,
You've pretty much got this done. There's just a couple of points.
First, ditch your new variable answer
. You don't need it.
Second use the expression you've created on tiles
using dot notation as you have done. Return the result of that expression.
Let me know how you get on.
Steve.
Jack Cummins
17,417 Pointspublic 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) {
// TODO: Add the tile to tiles
tiles += tile;
}
public boolean hasTile(char tile) {
// TODO: Determine if user has the tile passed in
return boolean hh = tiles.indexOf(tile) != -1;
}
}
Is this correct?
Steve Hunter
57,712 PointsNot quite.
Return what you are assigning into hh
.
The expression, as you know, evaluates to a boolean value. Just return it; don't assign it into anything. Remove boolean hh =
so you just return
the evaluated expression.
Steve.
Steve Hunter
57,712 PointsHi Jack - did you find the solution here or should I add some more help?
Steve.
Jack Cummins
17,417 PointsI found the answer. You helped me better than anyone has yet out of my many posts.
Steve Hunter
57,712 PointsJack, I'm always happy to help. You can @ mention me in here, if you want to get hold of me. I do have a day job and we may be in different time zones, but I'll answer as soon as I can.
Jack Cummins
17,417 PointsWhere do you live? Does that mean that I can put @ Steve Hunter into my question whenever I want you to answer my questions?
Jack Cummins
17,417 PointsRight now, in my time zone, it's 4:15pm.
Steve Hunter
57,712 PointsI'm in the UK - 21:42 here now. So we're a good few hours apart!
Jack Cummins
17,417 PointsJack Cummins
17,417 PointsI don't get what you mean when you say "Second use the expression you've created on tiles using dot notation as you have done. Return the result of that expression."
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsOK - you created an expression -
.indexOf(tile) != -1
. that's correct. Chain it ontotiles
using the dot notation just like you've chained it toanswer
. So replaceanswer
withtiles
. Last, add the keywordreturn
before all that.So, the method will have one line in it. It starts with
return
then has the above expression after that.Steve.