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 trialAra Brutian
565 PointsI don't know how to play scrabble so can't catch the logic of the excercise.
I dont understand what to apply the indexOf method to.
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(hasTile = getTiles.indexOf(tile) != - 1) {
return true;} else {
return false;
}
}
}
1 Answer
Livia Galeazzi
Java Web Development Techdegree Graduate 21,083 PointsAll you need to know is, the Scrabble player has tiles, and each tile is a character. Later the player can use the tiles to form words and earn points.
In the hasTile(char tile) method, you want to find out if the player already has a tile with that character or not. You're almost there.
tiles.indexOf(tile) will check the string 'tiles' and return the index of the character 'tile' in the string. If 'tiles' does not contain this character at all, the method returns -1. So you just apply indexOf to the 'tiles' string, and check if the result is -1.
public boolean hasTile(char tile) {
// TODO: Determine if user has the tile passed in
return tiles.indexOf(tile) != - 1
}
Or this works just as well, using the getter instead of directly using the variable name. Don't forget the brackets, because it's a method.
public boolean hasTile(char tile) {
// TODO: Determine if user has the tile passed in
return getTiles().indexOf(tile) != - 1
}
This works in theory but is the wrong solution to the exercise because it adds unnecessary code. It does exactly the same thing than the above with more lines of code.
public boolean hasTile(char tile) {
// TODO: Determine if user has the tile passed in
if (getTiles().indexOf(tile) != - 1) {
return true;
} else {
return false;
}
}
In your case, the "hasTile =" that you added in the brackets causes a compiler error. You don't need it! tiles.indexOf(tile) != - 1 is a boolean expression. It is either true or false. You can return it directly without the need to put it in a variable or to add an if block.
Guanhua Ding
Courses Plus Student 2,679 PointsGuanhua Ding
Courses Plus Student 2,679 PointsThanks for your answer. I got it right, just not sure what the question actually wants. The description is a little bit confuse.