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 trialGem Knight
4,779 PointsI am unsure on how to do this lesson. Can I please have some help.
I am on Java Objects, and I am not sure what is the flaw in my code. Can somebody please help me.
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) {
return mHand.indexOf(tile) > -1;
}
public Integer getTitleCount() {
for (char letter : mHand.toCharArray()) {
int matches += 0;
if (letter == mHand.indexOf(tile)) {
return matches += 1;
}
}
}
}
1 Answer
jcorum
71,830 PointsWhat the challenge wants is a method you can pass a tile to, say 'M', and the method will tell you how many 'M's there are in the player's hand. So you need to have a char parameter for your method.
Then you loop through the player's hand, and each time a tile in the hand matches the tile passed in, you increment count.
public int getTileCount(char tile){
int count = 0;
for (int i = 0; i < mHand.length(); i++) {
if(mHand.charAt(i) == tile){
count++;
}
}
return count;
}
Note, it has to be a loop, not an if...else..., as the player could have as many as 7 (or 9) tiles.