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 trialIfueko agbontaen
1,928 Pointsplease help me fix this code
;-{
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)){ return true;
}else return false;
}
}
1 Answer
Wikus van der Westhuizen
Courses Plus Student 4,335 PointsWe need a bit more information as to what part of your code you need help with. I'm guessing that you're referring to the compile error in your "hasTile" method.
The following statement returns a numeric value of the location of the tile (char) in tiles (String): tiles.indexOf(tile)
The if statement needs a boolean expression (something that returns true of false). Luckily the "indexOf" method returns a -1 if no match was found. Thus you can change your code accordingly:
if(tiles.indexOf(tile) != -1)
{
return true;
} else {
return false;
}
Since we are only interested in the result of the if statement we can shorten our code:
return tiles.indexOf(tile) != -1;
We are asking tiles (String) where in it's list is tile (char). If it returns -1 it means that the tile (char) is not listed in tiles (String).
For more information you can have a look documentation provided by Oracle: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int)
Ifueko agbontaen
1,928 PointsIfueko agbontaen
1,928 PointsGod!!!!!!! thank you so much