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

Stage 3 : Creating the MVP, Challenge Task 1 of 1.

This is the error I am receiving: Bummer! The hand was "sreclhak" and 'e' was checked. Expected 1 but got 8.

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

https://teamtreehouse.com/library/java-objects/creating-the-mvp/for-each-loop

Can you link the video or the challenge? Thanks!

Ken Alger
Ken Alger
Treehouse Teacher

Ronald;

Is this the challenge to which you are referring? If so, it is a Java course, not an Android course. Anyway, post back and someone can certainly answer your question. Also, if you post the code you have tried it will help to see where the issue is.

Thanks,

Ken

4 Answers

Hey Ronald! You're using the for each loop correctly but it looks like there is a little bit of flawed logic in your if condition. The way you have it set up right now, you're essentially getting each tile in the hand, and then checking to see if the tile is in the hand (and incrementing tileCount if it is). This is why the test example from the workspace received 8 instead of 1.

What you want to do instead is look at each tile in the hand (your for each loop does this) and check to see if the letter being passed into the method matches the tile we are looking at (this is your if condition). If it is, then increment tileCount. This should accurately represent the number of times the letter shows up in the tiles in the hand.

Hope this helps! Feel free to let me know if you have further questions.

Welcome to a little Twitter-like application. Here I'm modeling a Tweet. It has text which is the body of the tweet. All Tweets have a maximum character limit of 140 characters.

Please add a new constant to store the maximum number of characters allowed which is 140. Use the proper access level modifiers to make it unchangeable and accessible right off of the class. Follow the proper naming convention.

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

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

ts tough tel us exactly your problem