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 trialAndrei Oprescu
9,547 PointsCan you help me with this question?
Can you help me with what variable I should use? As I am going through the 'java' objects course I have been given this question:
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.
I understand What I am supposed to do but I don't know what variables I should use. Currently I have 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) { tiles += tile;
}
public boolean hasTile(char tile) { boolean hasTiles = tile.IndexOf(tile) != -1; return false; }
} I am working on the line 19 ( It sais "boolean hasTiles = tile.IndexOf(tile) != -1;")
do you think you could help and report any mistakes please?
Andrei.
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) {
tiles += tile;
}
public boolean hasTile(char tile) {
// TODO: Determine if user has the tile passed in
return false;
}
}
4 Answers
Tornike Shelia
2,781 Pointsreturn tiles.indexOf(tile)>=0;
This is the solution, this will return index number of tile in the tiles , if tile is in tiles then index number will be greater than or equal 0 and this statement will return true, If tile is NOT in the tiles the index number will be -1 and the statement will return false because -1 is not greater than or equal to 0 . Hope this cleares it up , if you have any question feel free to ask .
This statement goes inside the hasTile method.
By the way, you can achieve the goal with many different ways, for example you can use if statement to determine IF the tile is in the tiles and then return true if it is , like this :
if(tiles.indexOf(tile)>=0){
return true;
}
return false;
This will return true if tile is in tiles and return false if it is NOT . But , our teacher asks us to "practice returning the result of the expression that uses the index of a char in a String." , So thats why I used the different solution.
Andrei Oprescu
9,547 PointsHi again,
Am I supposed to use a new string that I should make? At the top of the code It says : // A String representing all of the tiles that this player has
Does that mean I should make a new String?
Andrei
Tornike Shelia
2,781 PointsHi Andrei , no the comment which teacher left for us is just saying that :
private String tiles;
this is the string which represents all of the tiles that the player has.
Andrei Oprescu
9,547 PointsThanks!
Tornike Shelia
2,781 PointsNo problem , If my answer helped you to solve the problem and uderstand it , I would appreciate it if you would mark it with green tick :)
Che Perth
1,989 PointsI finally figured it out.
public boolean hasTile(char tile) {
boolean isHit = tiles.indexOf(tile) != -1;
return isHit;
}