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

Sérgio Haruo Hattori
Sérgio Haruo Hattori
5,867 Points

Scrabble - MVC - for LOOP - ERROR java.lang.reflect.invocationonTargetException

I am having the following error, I don't know how to solve this using the for each loop as "for(char letter : mHand.toCharArray()) {....."

I could solve the problem using: "public int getTileCount(char tile) { int count = 0; for (int i = 0; i < mHand.length(); i++) { if (mHand.charAt(i) == tile) count++; }
return count; }"

but i would like to know how to solve the challenge using both methods. Thanks =)

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

  public boolean hasTile(char tile) {
   return mHand.indexOf(tile) > -1;
  }
}

2 Answers

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

Hi Sérgio,

public int getTileCount(char tile) {
    int count = 0;
    for (char letter : mHand.toCharArray()) {
        if (mHand.charAt(letter) == tile) //this line
            count++;
    }   
    return count;
}

Looking at the line above, you are using the String mHand and trying to get the charAt passing in a char as an argument. For that method you should be passing in an int. You are sort of saying "from the String mHand, get the character at index 'c'" Instead of "from the String mHand, get the character at index '3'"

Now, because you are using the enhanced for loop, you don't need to call that method. Each char is being passed to you via the "letter" variable. So your code should look like:

public int getTileCount(char tile) {
    int count = 0;
    for (char letter : mHand.toCharArray()) {
        if ((letter) == tile) //fix error -- you could also have if (letter == tile)
            count++;
    }   
    return count;
}

I hope this helps, if not let me know.

Sérgio Haruo Hattori
Sérgio Haruo Hattori
5,867 Points

Hi Thanks! it solved the problem, only one correction needed to be made in your answer, I had to change this:

if (letter) == tile)

to this:

if ((letter) == tile)

It was missing the parenthesis.

But now I understood where I made the mistake! Thanks =D

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

Good catch, I missed it when I was replacing the line.