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 trialJessie Burton
5,198 PointsLost, don't know what to do
I wish the questions kept in mind we are total beginners.
Okay great, now can you fix the hasTile method for me, right now it always returns false.
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 is 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;
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 hasTile = tiles.indexOf(tile)
if (hasTile = -1) {
return false;
} else {
return true;
}
}
}
Michael Stedman
Full Stack JavaScript Techdegree Student 13,838 PointsI am also wondering if there is a type in the last part of the instructions that might be adding to my confusion. It states:
"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 is uses the index of a char in a String."
The part that says, "... is uses the index..." just seems like it sounds weird. Maybe I have been staring the screen, and ultimately the same problem, for too long. "... is uses..." just sounds like there might be a typo of some sort. Could you let me know either way, Craig Dennis ? Thanks
Craig Dennis
Treehouse TeacherThe code here would pass and give you the suggestion to drop the if statement. However, you forgot the double equals in your expression, single equals is for assignment.
Craig Dennis
Treehouse TeacherHa, yes that is a typo, thank you! Will fix it.
Jainesh Patel
11,100 Pointswhat I got for my solution was this
Solution:
boolean correct = tiles.indexOf(tile) != -1; return correct;
6 Answers
Michael Stedman
Full Stack JavaScript Techdegree Student 13,838 PointsJessie Burton So.... Finally figured it out. Utilizing the corrected typo in the instruction sentence, and the several comments from Craig Dennis I was able to fiddle around and get the answer. Literally, it was just a bunch of trial and error and piecing things together from the comments Craig made. I'm going to go ahead and paste the code that I got that allowed me to pass the second objective as ONLY after looking at it (after I knew it passed) did I actually understand how it was what Craig was talking about. Finally getting to see it is what gave me the opportunity to understand it as asking on here I have to wait for answers to pop up, and then some are not always what the exact answer needed. Thanks for a tough one, Craig Dennis!
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 tiles.indexOf(tile) >= 0;
}
Huseyin Erkmen
11,268 PointsYour answer is totally true and simple. I tried with if else and it warned me to try without if else statements. Thank you.
ababab ababab
986 Pointsthanks man
Jeffrey Lewis
Courses Plus Student 6,028 PointsThe answer I got was boolean hasTile = answer.indexOf(tile) != -1; return hasTile;
Jacob Archambault
27,524 Pointspublic boolean hasTile(char tile) {
// TODO: Determine if user has the tile passed in
return tiles.indexOf(tile) != -1;
}
markmneimneh
14,132 PointsMichael Stedman
I am trying to explain the to the requester that if the method is expecting a boolean return value, then he/she can't return an int. This is the core of the rro made, and this is more important to explain than what the quiz wants or does not want.
Michel Bourgeois
731 PointsCraig Denis you mean eliminate the if statement completely????? in that case we must replace it with another condition statement!
Craig Dennis
Treehouse TeacherReturn the result of the expression inside the parenthesis
markmneimneh
14,132 PointsHello
The problem is right here
public boolean hasTile(char tile) { // TODO: Determine if user has the tile passed in boolean hasTile = tiles.indexOf(tile) if (hasTile = -1) { return false; } else { return true; } }
specifically ... boolean hasTile = tiles.indexOf(tile)
tiles.indexOf(tile) returns -1
-1 is not a boolean per say .... there is a science behind this ... but for now, assume a boolean is true/false. tiles.indexOf(tile) returns an int
I would replace the whole function with
public boolean hasTile(char tile) { // TODO: Determine if user has the tile passed in if (tiles.indexOf(tile) >= 0) { return true; } else { return false; } }
If this answers your question, please mark the question as answered
Thanks
Michael Stedman
Full Stack JavaScript Techdegree Student 13,838 PointsMark Mneimneh - While your suggestion/answer may possibly work, it does not come up as a correct answer. Entering the following (which is what you suggested to Jessie Burton to use, and that I will put in code format for ease of reading) comes up as a "wrong" answer, but the description of why it is "wrong" says, "While you could definetly solve this using an if statement, try returning the result of the expression."
So, I am not sure if this is actually a "wrong" answer, or if it would actually work. I see that you have several courses under your belt, so I hope you (or someone else) could come up with an answer that will actually get the green light. I know I sure could use the help on this one lol.
public boolean hasTile(char tile) {
// TODO: Determine if user has the tile passed in
if (tiles.indexOf(tile) >= 0) {
return true;
} else {
return false;
}
}
Craig Dennis
Treehouse TeacherThe expression is inside of the if statement between the parenthesis. That expression itself returns true
or false
, I'm trying to encourage you to see that you can return the result of that expression without adding the if
ceremony around it.
The if statement isn't wrong per se, but it is unnecessary, and probably wouldn't pass a code review.
Michael Stedman
Full Stack JavaScript Techdegree Student 13,838 PointsMichael Stedman
Full Stack JavaScript Techdegree Student 13,838 PointsI was thinking the same thing regarding keeping in mind the experience level most likely being beginners. I have done pretty well in all the other code challenges, but this one has surely stumped me. Another thing that I am sure that might have a slight hurdle for me is that I have never played Scrabble before. Lol As American as I am, I know what the game is, but just have never played it and know very little about it.
I know Craig Dennis does a swell job of providing thought provoking, and challenging challenges but I have seriously been stuck on this one for more than thirty minutes. This whole "Creating the MVP" section of the course, to me anyway, does feel like the difficulty level ramped up quickly. Which is good, but I just cannot find the solution to the second objective of this first Code Challenge.