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

So back to that ScrabblePlayer. I found that it's not enough to know if they just have a tile of a specific character. W

how I return count ?

So back to that ScrabblePlayer. I found that it's not enough to know if they just have a tile of a specific character. We need to know how many they actually have. Can you please add a method called getTileCount that uses the for each loop you just learned to loop through the characters and increment a counter if it matches? Return the count, please, 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 boolean hasTile(char tile) {
   return mHand.indexOf(tile) > -1;
  }
  public String getTileCount() {
    for (char tile: mHand.toCharArray()) {
      if(mHand.indexOf(tile) >=0) ;
    }
}
Andrew Nguyen
Andrew Nguyen
2,470 Points

Hi, if your getTileCount does your method return a String or an int? If it is an int then you can simply do.

public String getTileCount() {
    int counter = 0; 
    for (char tile: mHand.toCharArray()) {
      if(this.hasTile(tile)){
        counter++; 
      } 
    }
    return counter; 
}

How ever if you are returning a string then:

public String getTileCount() {
    int counter = 0; 
    for (char tile: mHand.toCharArray()) {
      if(this.hasTile(tile)){
        counter++; 
      } 
    }
    return Integer.toString(counter);
}

thanks to all of you

4 Answers

Miroslav Kovac
Miroslav Kovac
11,454 Points

Do you mean something like this?

public int getTileCount(char tile) {
    int count = 0;
    for (int i = 0; i < mHand.length(); i++) {
        if (mHand.charAt(i) == tile) 
            count++;
    }   
    return count;
}
Craig Dennis
Craig Dennis
Treehouse Teacher

That will work, you can use a for-each loop to loop through the hand too

for (char letter  : mHand.toCharArray()) {

the only think that I don't understand in regards to this logic is that the char variable tile is not initialized so how can it be compared to a method or even another variable if the tile variable has no value

ammmm now I understand what is going on
thanks Craig ..

I'm having the same problem and I got confused with all these different answers but what line would I put the, for (char letter : mHand.toCharArray()) {

Steve Fielding
Steve Fielding
7,287 Points

you can try this <code> public int getTileCount(char tile) { int count = 0; for(char letter : mHand.toCharArray()) //Because we want to compare each tile with the actually tile in hand if(letter == tile) count++; </code>

Teferi Heye
Teferi Heye
3,448 Points

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

}
return count;

}