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

cant understand question

i dont get it

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

2 Answers

Stone Preston
Stone Preston
42,016 Points

We are tasked with looping through each character in the hand and checking to see if the character in the hand matches the character thats passed in to the function. If matches, we need to increment a counter variable.

you start by defining the function. it returns an int (the count of the matching tiles in the hand) and should take one parameter, the tile you wish to get the count of

public int getTileCount(char tile){

  }

then we need to create a counter variable to store the count in. you want to initialize this outside of the loop. if you create it inside the loop, it wont count correctly

public int getTileCount(char tile){
    int count = 0; 

  }

then loop through the mHand variable converted to an array of characters. we can create a working variable named tileInHand for use inside the loop

public int getTileCount(char tile){
    int count = 0; 
    for (char tileInHand : mHand.toCharArray()){

    }

  }

inside the loop we need to see if the working variable tileInHand equals the tile passed in to the function. If they are equal, we can increment the count. you can increment using the postfix ++ operator

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

  }

finally, you can return the count outside of the loop at the end of the function. this ensures the count is returned after all looping is complete

public int getTileCount(char tile){
    int count = 0; 
    for (char tileInHand : mHand.toCharArray()){
      if (tileInHand == tile) {
         count++;
       }
    }
    return count;
  }
Tom Schinler
Tom Schinler
21,052 Points

Great breakdown!!!! Thanks!

you probably want a { } around that if just for readability.. and you also don't want int i ... you want to initialize the i outside the foreach loop (just before) to 0. and then do whatever you are doing..