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

Nicole Favela
Nicole Favela
1,342 Points

I'm confused by the question. What am I doing wrong?

the instructions say to loop through the chars in the method and increment a counter if it matches. What is it?? Am I looking for a specific letter?

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 (tile == 'A') {
        Count++;
      }

return Count;

    }
    }

2 Answers

Damien Watson
Damien Watson
27,419 Points

Hi Nicole,

You are pretty close, the character you need to check against is the 'tile' argument which is passed in to the method. In your 'for' loop, you need to have a different variable name which then checks against what is passed in.

The only other thing is the 'return Count' should be outside of the 'for loop', otherwise it will only be called once and then return Count at what ever it got to.

  public int getTileCount(char tile) {
    int Count = 0;
    for (char checkTile : mHand.toCharArray()) {
      if (tile == checkTile) {
        Count++;
      }  
    }
    return Count;
  }
Nicole Favela
Nicole Favela
1,342 Points

Thanks! I didn't know you could add a new variable without declaring it beforehand. I still don't know what the question means when it says increment a counter if it matches. If it matches with what? What is checkTile checking for?

Damien Watson
Damien Watson
27,419 Points

Ok, I'll break it down. In the 'for each' loop, 'mHand' is being broken up into an array of characters which gets looped through, 'checkTile' is assigned the character in current focus.

So in the following situation, the 'for' loop would loop 5 times (one for each character).

tile = 'l';   // Character passed in is a lowercase 'L'
mHand = "hello";
// Loop 1 -> checkTile = the first character
checkTile = 'h';
if ('l' == 'h') Count++;   // false
//Count == 0;

// Loop 2 -> checkTile = the second character
checkTile = 'e';
if ('l' == 'e') Count++;   // false
//Count == 0;

// Loop 3 -> checkTile = the third character
checkTile = 'l';
if ('l' == 'l') Count++;   // true
//Count == 1;

// Loop 4 -> checkTile = the fourth character
checkTile = 'l';
if ('l' == 'l') Count++;   // true
//Count == 2;

// Loop 5 -> checkTile = the fifth character
checkTile = 'o';
if ('l' == 'o') Count++;   // false
//Count == 2;

Does this help explain it?

Nicole Favela
Nicole Favela
1,342 Points

yeah, that example really helped simplify it. I appreciate it. that helped end my hour long headache. lol

Damien Watson
Damien Watson
27,419 Points

Cool, I love it when something finally makes sense. :)