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 trialkastriot ramadani
691 PointsHelp, I'm stuck at Srabble tiles Task 2!
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.
I tried that method that is written in the code but it says: Bummer! While you could definitely solve this using an if statement, try returning the result of the expression.
Thank you.
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
if (tiles.indexOf(tile) >= 0) {
return true;
} else {
return false;
}
}
}
6 Answers
markmneimneh
14,132 PointsBasically, you could do this:
return tiles.indexOf(tile) >= 0)
instead of
if (tiles.indexOf(tile) >= 0) { return true; } else { return false; }
if this answers your question, please mark the question as answered.
Cristian Ilie
3,289 PointsThis worked, Thank you !
return tiles.indexOf(tile) >= 0;
Carlos Salcedo
1,788 Pointsworked perfect, and had i had no idea how else to do it except for the if statement, thanks alot
Charlie Flowers
612 PointsHow do you do this I'm stuck on it?
ondrejhavazik
1,577 PointsCan someone explain me the part, where I have to use ">= 0"?? .... Why simple tiles.indexOf(tile) don't work??
Craig Dennis
Treehouse TeacherHi ondrejhavazik !
indexOf
will return -1
if the value is not found in the String.
Does that make sense now why it has to greater than or equal to 0 (zero is the first character)?
chase singhofen
3,811 Pointsi was lost with this one. i was using if statements.
with this workspace you must be careful not to add or delete any brackets. it doesnt work the same as an IDE. the syntax errors dont do much either it just points with the " ^ "
Raivis V
551 Pointsreturn tiles.indexOf(tile) >= 0;
worked good for me,
Thank you
Warren Lourens
625 PointsThanks
Craig Dennis
Treehouse TeacherCraig Dennis
Treehouse TeacherWhat's the expression? That's the part you've got in the
if
statement between the parenthesis.