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 Remaining Tries

kabir k
PLUS
kabir k
Courses Plus Student 18,036 Points

My method is returning the integer 8 instead of 1

In the For Each Loop Challenge, I am getting the following error for the method I've written

Bummer! The hand was "sreclhak" and 'e' was checked. Expected 1 but got 8.

What am I doing wrong?

Here's my getTileCount() method

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

I'm stuck on the same problem. Can't really see where I'm going wrong here :(

3 Answers

Hi there,

The method needs to take a char as a parameter. The for:each loop then iterates through the mHand string and counts how many instances of that char occur. So, if the player's hand was H E L L O, the method would be called like: getTileCount('L'); and it would return 2, the number of Ls in the hand.

My solution looks like:

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

So, the local variable letter will contain each character of the mHand string one at a time. The loop iterates through each letter, storing it in letter. We then compare letter to the character we passed in as a parameter, tile, and add one to the counter if the two are equal.

I hope that makes sense!

Steve.

kabir k
kabir k
Courses Plus Student 18,036 Points

Ah! I knew I had the tile and letter characters but I wasn't thinking clearly to compare the two. As I thought I had to use the condition in the if statement.

Thanks, Steve!

Evan Tidwell
Evan Tidwell
7,608 Points

Hey Steve,

Why would this solution not return an error for not initializing our char letter variable? Is it initialized in the if-statement?

Hi Evan,

The letter variable is initialized at the start of the for loop; where it starts with char letter.

The variable is local to the loop and holds no scope outside of it.

Make sense?

Steve.

Your for loop runs for every letter in the array once. Your string, which gets converted to an charArray in the condition, has 8 letters. Your for loops runs 8 times and "checks" if the index of 'e' is >= 0. Thats true...for every time the for loops runs. So you add 8 times 1 to the variable count. So count is 8.

kabir k
PLUS
kabir k
Courses Plus Student 18,036 Points

I believe the reason why I'm getting 8 instead of 1 is that besides not doing the comparison between the tile and letter characters, for every iteration of the character array, mHand, the tile is in the array which is basically what is counted 8 times resulting in 8 and not 1

Yes, because 'e', the char you chose, is above position zero (you're testing for its position being greater than zero), the 'if' condition is always true. So, the count += 1 line runs every time the loop iterates. So your count result ends up as measuring the length of the mHand string as that is what limits the number of loops.

Make sense?

Steve.