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 trialJames N
17,864 Pointsi need help with this codeChallenge!!
can someone please help me? thanks in advance!! i keep getting the following errors:
./ScrabblePlayer.java:23: error: not a statement
for (tile : mHand.toCharArray()) {
^
./ScrabblePlayer.java:23: error: ';' expected
for (tile : mHand.toCharArray()) {
^
./ScrabblePlayer.java:23: error: illegal start of expression
for (tile : mHand.toCharArray()) {
^
./ScrabblePlayer.java:23: error: ';' expected
for (tile : mHand.toCharArray()) {
^
./ScrabblePlayer.java:23: error: illegal start of expression
for (tile : mHand.toCharArray()) {
^
./ScrabblePlayer.java:23: error: ')' expected
for (tile : mHand.toCharArray()) {
^
./ScrabblePlayer.java:23: error: illegal start of expression
for (tile : mHand.toCharArray()) {
^
./ScrabblePlayer.java:23: error: not a statement
for (tile : mHand.toCharArray()) {
^
./ScrabblePlayer.java:23: error: ';' expected
for (tile : mHand.toCharArray()) {
^
9 errors
my code is:
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 int getTileCount(char tile) {
int tiles = 0;
for (tile : mHand.toCharArray()) {
if (mHand.indexOf(tile) >= 0) {
tiles++;
}
}
return tiles;
}
}
1 Answer
Craig Dennis
Treehouse TeacherYou need to specify the datatype in the for each loop.
for (DATATYPE variable : array) {
Lee Reynolds Jr.
5,160 PointsLee Reynolds Jr.
5,160 PointsThank you for the generic code Craig. It was very helpful to my hindsight.
James N
17,864 PointsJames N
17,864 PointsI am pleased to know that others are being helped from my forum requests, however I do not completely understand this. the variable "tile" is already specified as a char. so why do I need to put the variable's datatype in the for each loop?
also, if I change the new variable name in the for each loop, it says: Bummer! The hand was "sreclhak" and 'e' was checked. Expected 1 but got 8.
Craig Dennis
Treehouse TeacherCraig Dennis
Treehouse TeacherYou are creating a new variable that is being used in this loop. You're right,
tile
is passed in already, this one could be called something likechar letter
. In the loop the equality check between tile and letter should occur.That make sense?
James N
17,864 PointsJames N
17,864 Pointsit does make sense, however in case you didn't notice the update in my previous comment:
"also, if I change the new variable name in the for each loop, it says: Bummer! The hand was "sreclhak" and 'e' was checked. Expected 1 but got 8."
do I change "tile" in the if statement to this new variable called "letter"???
thanks for the help so far though!!!!!!!!
PS: you are a great teacher.
Craig Dennis
Treehouse TeacherCraig Dennis
Treehouse TeacherHINT: Only increment the count if tile and letter are equal. Looks like you are incrementing on every letter.
PS: Thanks!
James N
17,864 PointsJames N
17,864 PointsThanks and your welcome! I finally completed that code challenge!!
thanks!!