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 trialMatthew Philip Uy
3,615 Pointsis this really correct?
is there a more simpler or correct code other than this?
public class ScrabblePlayer {
private String mHand;
public ScrabblePlayer() {
mHand = "";
}
public String getHand() {
return mHand;
}
public void addTile(char tile) {
// Adds the tile to the hand of the player
mHand += tile;
}
public boolean hasTile(char tile) {
boolean hasTile = mHand.indexOf(tile) < 0;
if (!hasTile) {
return true;
}
return false;
}
}
2 Answers
jcorum
71,830 PointsFor hasTile() you need something more like this:
public boolean hasTile(char tile) {
return mHand.indexOf(tile) > -1;
}
indexOf(tile) will return the position of the char if it is in mHand, otherwise it will return -1. So mHand.indexOf(tile) > -1
is a boolean expression and can be returned directly, as the return type is boolean.
jcorum
71,830 PointsYou are right. >= 0 does work!! Don't know what I was thinking. Guess I shouldn't answer questions late at night. Wow!
Matthew Philip Uy
3,615 PointsI'm a bit confuse... Is 0 > -1 really false? I tried it on java-repl and it says true... can you further explain why is it false?
Matthew Philip Uy
3,615 PointsMatthew Philip Uy
3,615 Pointscan this be use? return mHand.indexOf(tile) >= 0;
if not, why not? (sorry for asking a lot lol)