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

why use a for loop to get the number of tiles in the hand?

there is a command to get the length of a string....

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 getTileCount(){
    for (
  }
  public boolean hasTile(char tile) {
   return mHand.indexOf(tile) > -1;
  }
}

3 Answers

Kevin Faust
Kevin Faust
15,353 Points

Great explanation Grigorij Schleifer !

I also now understand what Eli was confused about after re-reading his question.

why use a for loop to get the number of tiles in the hand?

there is a command to get the length of a string....

We don't want to know the total number of letter that we have. If we wanted to find that, then of course we can use the length method. But the challenge says it specifically wants to know how many of a specific letter is in our mHands string

Kevin Faust
Kevin Faust
15,353 Points

Well we want to know how many of a specific character there are in our mHands string. What other way would be able to do this?

Change our mHands to a char array and loop through it and check if the tile we pass in matches to the current char we are looping on.

Since you didn't actually ask how to do the challenge itself I will believe that you are able to do it. If you need help, let me know and we can go through it in depth !!

Kevin

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Eli,

allow me to explain you this challenge a little bit. Kevins explanation is as always great, let me add a little bit code to it :)

public int getTileCount(char tile) {
// your getTileCount accepts a char "tile"
// you can then compare this accepted char with the letter inside the for-loop

int count = 0;
// here you need to initialize an int, because it is a local variable and you need it to be increase every time 
// if your tile that you give to your method is equal to the letter

 for(char letter: mHand.toCharArray()){
// you can read the for-loop like: for every char "letter"" inside mHand
// mHand is a String but you are comparing chars, so you convert your mHand to an array of chars

 if (letter == tile) {
// this is a condition
// if a tile you give to the method is equal to letter inside mHand array 

count++;
// the count will be increased
      }
    }

return count;
// the getTileMethod should return an integer
  }

I hope we could help

Grigorij