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

janeporter
PLUS
janeporter
Courses Plus Student 23,471 Points

can't figure out what to do here.....

public int getTileCount() { int count = 0; for (char tile : mHand.toCharArray()) { if (mHand.indexOf(tile) >= 0) { count += 1; } } return count; } }

What am I doing wrong??? It's saying that the method is not defined...Looks like it's defined to me...

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

4 Answers

Have you matched all your parentheses? Without seeing your code, sounds possibly like you might be missing a parenthesis or bracket. If that's not it, try pasting your updated code again so we can take a look.

janeporter
janeporter
Courses Plus Student 23,471 Points

I think that was the issue. It's now working. Thank you for your help.

To clarify, the exercise appears to be asking, not for the number of tiles (letters) in the Scrabble hand, but for the number of letters of a specific type. For example, how many tiles of the letter B does the hand have. In order to answer that question you must provide to the getTileCount() a parameter, say char tile, and then count the number of such tiles in the hand that match it. It's probably saying it's not defined because you need to define it as getTileCount(char tile) and then rewrite the code to count the matches.

janeporter
janeporter
Courses Plus Student 23,471 Points

i did that and got a compiler error... /ScrabblePlayer.java:24: error: variable tile is already defined in method getTileCount(char) for (char tile : mHand.toCharArray()) { ^ 1 error

Change one or the other to some other variable, otherwise the compiler thinks you are trying to define the same variable twice. Call the parameter you are passing in t instead of tile and rewrite your algorithm. You need to increment the count when the tile in the hand equals the t (tile) you passed in as a parameter, because then you've found another matching tile in your scrabble hand. (Replace the "mHand.indexOf(tile) >= 0" with "t == tile")

janeporter
PLUS
janeporter
Courses Plus Student 23,471 Points

public int getTileCount(char tile) { int count = 0; for (char ti : mHand.toCharArray()) { if (tile == ti) { count += 1; } } return count; i'm now getting the following error...

/ScrabblePlayer.java:30: error: reached end of file while parsing } ^ 1 error

what's missing now???