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 trialdjwyatt
13,954 PointsStrings and Char Objective #2
Can you please fill out the addTile method so that it takes the char that is passed and adds it to the mHand member field? Thanks!
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) ;
return false;
}
}
1 Answer
Grigorij Schleifer
10,365 PointsHi there,
This is a funny challenge:
The method hasTile takes a char that you compare with every char inside of the String mHand. The problem is that String is not of a char type. String typeis a concatenation of characters. So you need to build a char array from the mHand String and compare every char with your parameter "tile".
To compare every single unit of something you can use a for loop. Inside this loop you are assigning a character from mHand to a new char variable. And compare this new character with tile.
Look at this code:
// TASK 1
public void addTile(char tile) {
// Adds the tile to the hand of the player
mHand += tile;
}
// TASK 2
public boolean hasTile(char tile) {
// the method take a char that you will compare with every single char from the String mHand
for (char charFromHand: mHand.toCharArray()) {
// here you are making a character array
if (charFromHand == tile) {
// if a char from mHand is equals to tile
// return true
return true;
}
}
return false;
}