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 trialWilliam J. Terrell
17,403 PointsScrabblePlayer For Each Loop Challenge
In all honesty, I don't even know how I've made it this far in the course. I seem to do perfectly well at following along, but I feel like the concepts continuously escape me.
I think I have the basis for what is required of this challenge in the code below; I left a //comment to show where it is, but I can't figure out what to pass in...
Any assistance would be appreciated.
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) > -1;
}
//Here is what I have thus far...
public int getTileCount() {
int tileCount = 0;
for () {
tileCount++;
}
return tileCount;
}
}
2 Answers
Youngchul Kim
4,258 PointsBased on what you have done so far, I'm guessing you must be stuck at how to use the for-loop. I was kind of bummed out on this one as well, because I didn't know the syntax for the "Enhanced" loop. See this link or link2 for quick syntax reference.
And please look below code from previously discussed thread. I've just added comments to help you (me) read it :)
/*pass char named theTile to getTileCount class
set initial count as 0
put whatever is in mHand into the toCharArrary() to char placeholder named tile
compare the placeholder tile with user passed char named theTile
increment the counter if it has a match
//otherwise, just pass the count.
*/
public int getTileCount (char theTile) {
int count = 0;
for (char tile : mHand.toCharArray()) {
if (tile == theTile){
count++;
}
}
return count;
}
As you can see, you weren't far from completing the challenge.
ISAIAH S
1,409 PointsThis post may be helpful.
William J. Terrell
17,403 PointsWilliam J. Terrell
17,403 PointsSo... we make getTileCount a method that takes a char value that we're naming theTile, then we start a counter (at zero), and we say "convert the value in mHand (which starts off as a string) to a char array and compare it to the char value in 'tile'". Then, compare that value (which will be a char) to the value of theTile, and if they match, return...however many match? (so if the player has a 'T', it compares, and if it sees another 'T', it returns that 'T' plus the additional 'T', and continues through, and if it sees another 'T', returns that, and so on and so on...?) And if it doesn't find any additional tiles that match any of the chars in the CharArray generated from mHand, it just returns the regular count...
(The code you left in the comment is much more concise; I'm just trying to see if I actually get what's going on ^^; )
Thanks!
Jay Sarigumba
9,038 PointsJay Sarigumba
9,038 Pointsso this is the code i needed: if (tile == theTile)
thank you! T_T