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

Benjamin Ing
Benjamin Ing
910 Points

syntax error

I am really struggling to understand this at all..

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 count = 0;
    for(char tile : mHand.toCharArray()) {
      if (mHand.indexOf(tile)) {
        count++;
      }
    }

  }

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Benjamin;

I looking at your code and running it through the challenge checker, I get three distinct syntax errors

errors.java
./ScrabblePlayer.java:28: error: reached end of file while parsing
  }
   ^

./ScrabblePlayer.java:22: error: variable tile is already defined in method getTileCount(char)
    for(char tile : mHand.toCharArray()) {
             ^

./ScrabblePlayer.java:23: error: incompatible types: int cannot be converted to boolean
      if (mHand.indexOf(tile)) {
                       ^
3 errors
  • The first error indicates that you have too many, or as is the case in your code, too few closing curly brackets. Add another one to the end of your code and that error will be fixed.
  • The second error is letting you know that you don't have to define tile as a Char type again. Try using a different variable name in your for loop other than tile. Something like tileInHand seems to be appropriate.
  • The third error is telling you that Java is having a problem with comparing the types. In this example, comparing an integer (int) to a Boolean value is problematic. Think about trying to resolve "3 is true."

Post back if that doesn't make sense. You are almost there with the challenge. A slight change in your if statement, along with fixing the items above, and I think you'll have it. HINT: Solving the if statement will resolve the third syntax error.

Happy coding,

Ken