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

I'm not sure on how to do the looping

How do i know if they have the tile and increment it ? I'm really lost.

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)  {
    for(char tile) {
      int Counter + 1; 
    }
    return Counter;

  }
}

3 Answers

The method needs to take a char as a parameter. The for:each loop then iterates through the mHand string and counts how many instances of that char occur. So, if the player's hand was H E L L O, the method would be called like: getTileCount('L'); and it would return 2, the number of Ls in the hand.

My solution looks like:

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

I hope that makes sense!

Steve.

This kind of makes sense, how comes you can call the member variable without declaring it.

You mean mHand? The method is held inside the class so every instance of the class can use the method and methods in each instance can access the member variables.

Does that make sense?

Steve.

Yes this makes sense, it's getting really difficult especially when on the HangMan class, declaring all the other stuff isn't really explained too well.

It should start getting easier as you get used to it all. There's a lot to take in!

Keep on going and just shout in the Community pages if you get stuck - there's always someone around to help.

Sorry trying to code this myself i like to expalain things through to help me understand the concept. You get the tile through the paramter char. You create a variable for counting purposes. This is the bit i'm not 100% sure on. You do a for each loop which letter stores the contents of mHand in a spaced out format. If the letter is the same as tile +1 each time.

Hi there,

This method is used to see how many times a certain letter is in the Scrabble hand. It is used by passing the letter you want to now about to the method; getTileCount('L');, for example.

Inside the method, the letter is received as the char that I called tile. We then set up a variable just to do the counting; I called it count because I lack imagination. ;-) We start with count being zero.

Next, we create a for:in loop. The loop iterates over mHand after that string has been turned into an array with the method toCharArray. The loop defines a local variable just for the loop - it is a char and I called it letter. This variable changes each time the loop runs. It holds the value of each letter in mHand in turn.

Let's stick with the example I used before (even though there aren't enough letters in it!!) and use "HELLO" as mHand. On the first pass of the loop, letter holds 'H'. This is then compared with tile that was passed in. If letter and tile equal the same thing, then we add one to count; count++. Else we carry on and do nothing.

Either way, we get to the next iteration; letter now hold 'E' and the loop goes on as before. If letter is the same as tile, count++ else do nothing. And repeat until we run out of letters in mHand.

OK with that?

Steve.

Ok thanks so much Steve, this is what makes the challengers harder in my opinion the lack of clarity of how the other code got there :I

Yeah thanks so much Steve, about the parameter where does it receive it's data from ?

You pass the parameter into the method so I guess that would depend how you implement the game.

It is only an example so the detail isn't really there.