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

Whitney Barber
Whitney Barber
9,453 Points

I'm not having any success with correcting the errors found in my scrabble player code, please assist.

MY CODE

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 String getTileCount() { String count = ""; for (char tile : mHand.toCharArray()) { char display = '_'; if (mHand.indexOf(letter) >= 0) { display = letter; } progress += display; } return progress; } }

ERROR MESSAGE

./ScrabblePlayer.java:25: error: cannot find symbol if (mHand.indexOf(letter) >= 0) { ^ symbol: variable letter location: class ScrabblePlayer ./ScrabblePlayer.java:26: error: cannot find symbol display = letter; ^ symbol: variable letter location: class ScrabblePlayer ./ScrabblePlayer.java:28: error: cannot find symbol progress += display; ^ symbol: variable progress location: class ScrabblePlayer ./ScrabblePlayer.java:30: error: cannot find symbol return progress; ^ symbol: variable progress location: class ScrabblePlayer 4 errors

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Whitney;

I don't see where you have initialized letter or progress. I think we are trying to do something like:

public int getTileCount(char tileToFind) {
    int tileCount = 0;
    for (char searchingTile : mHand.toCharArray()) {
      if (searchingTile == tileToFind) {
        tileCount++;
      }
    }
    return tileCount;
}

Does that make any sense? Post back if you are still stuck or the above code doesn't make sense.

Happy coding,
Ken

Whitney Barber
Whitney Barber
9,453 Points

I don't understand why searchingFile which is the amount of characters found in mHand matching tileToFind increments the tileCount.

Ken Alger
Ken Alger
Treehouse Teacher

Whitney;

Great question.

That for loop is going through each "tile" in mHand, right? Then in the if statement we look to see if the requested tile is in mHand, if it is we increment tileCount. So, if our hand has 3 of the letter "t", and we do getTileCount(t), we should expect our method to return 3. Does that help?

Ken

Whitney Barber
Whitney Barber
9,453 Points

Yes, your explanation is a huge help. Thanks again Ken!