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 trial

Java Java Objects (Retired) Creating the MVP For Each Loop

James N
James N
17,864 Points

i need help with this codeChallenge!!

can someone please help me? thanks in advance!! i keep getting the following errors:

output.html
./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:

ScrabblePlayer.java
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
STAFF
Craig Dennis
Treehouse Teacher

You need to specify the datatype in the for each loop.

for (DATATYPE variable : array) {
Lee Reynolds Jr.
Lee Reynolds Jr.
5,160 Points

Thank you for the generic code Craig. It was very helpful to my hindsight.

James N
James N
17,864 Points

I 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
Craig Dennis
Treehouse Teacher

You 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 like char letter. In the loop the equality check between tile and letter should occur.

That make sense?

James N
James N
17,864 Points

it 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
Craig Dennis
Treehouse Teacher

HINT: Only increment the count if tile and letter are equal. Looks like you are incrementing on every letter.

PS: Thanks!

James N
James N
17,864 Points

Thanks and your welcome! I finally completed that code challenge!!

thanks!!