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 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) {
boolean hasTiles = tile.IndexOf(tile) != -1;
return false;
}
}
6 Answers
Jonathan Grieve
Treehouse Moderator 91,253 PointsIt's a little tricky this one, but essentially you're being asked to write a condition statement, and return different things based on the result of the condition. So for one, you'll return true and the other you'll return false.
if(condition) {
return true;
} else {
return false;
}
michael lynch
9,560 PointsWith regards to the above code i dont think you need the if else statement. Lets look at it in detail, your declaring a method of type boolean, so this method is expecting you return either true or false. so when we use the method of indexOf() on tiles like so:
tiles.indexOf(tile) != -1;
We are checking here by using this indexOf() method if the tile is there within tiles, if it is not it will return a negative number and if true a positive. So we can now add an evaluator to this which is the not equals != so if the indexOf(tile) does not return -1(false) then it is true.
So all we need to do is use the return (this is called the result type) , so the method hasTile() wants a true or false value returned to it, so just put return in front of your statement.
return tiles.indexOf(tile) != -1;
Andrei Oprescu
9,547 PointsCan you give a bit more detail on what you mean? I have run this code and I have tried also to add more code but it is still not working.
Andrei
michael lynch
9,560 PointsHey,
Your setting the return to false manually. when you should be getting the return from the method itself of true or false.
public boolean hasTile(char tile) { return tiles.indexOf(tile) != -1; }
Andrei Oprescu
9,547 PointsIs there anything I am missing on?
This is where I'm at:
public class ScrabblePlayer {
private String tiles; private boolean isTile;
public ScrabblePlayer() { tiles = ""; }
public String getTiles() { return tiles; }
public void addTile(char tile) { tiles += tile; }
public boolean hasTile(char tile) { boolean isTile = tiles.indexOf(tile) != -1; if (isTile) { return true; return isTiles; } else { return false; } } }
Andrei Oprescu
9,547 Pointsno problem.
I solved the problem.
Andrei