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

Michael LaCroix
Michael LaCroix
5,828 Points

Lost

I'm just lost. Half of Craig's video made no sense. Help:(

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 count: mHand.toCharArray()){
    if (hasTile.indexOf(tile) >= 1)
      return count;
    }
  }

3 Answers

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey there Michael,

Let's break this down into steps.

The first thing that Craig wants us to do is create a method that returns an int called get Tile Count. that takes a char as a parameter ( i called my char variable 'tile' below)

We can do that with the following

public int getTileCount(char tile) {
}

Next, he wants us to use for each loop to have us loop through the letters in our hand and if they match the tile that we are using the method for, increase the count by one, like below.

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

Basically what we have done is created a brand new method to check and see if the character in our argument has passed, so if we type getTileCount and that character is in our hand that the count would increment by one, finally we would return the count so that we would know how many of that particular tile we have.

Thanks, let me know if this doesn't help.

Michael LaCroix
Michael LaCroix
5,828 Points

Sorry, don't see how you got that from the directions.

There's no indication that I'm supposed to add parameters. He didn't have to do it in the video. I don't understand why I need to.

The way I read the directions it sounded like I was supposed to return how many of each letter is in the player's hand and I have no idea how to differentiate those variables. How am I supposed to make it check all the entire alphabet and then indicate how many of each letter? Clearly your code isn't doing that so I totally misunderstood the directions.

As I understand your code it looks like all your doing is counting the number of tiles in the player's hand. I must be missing something, so please explain.

Thanks

Michael LaCroix
Michael LaCroix
5,828 Points

oooohh - are we asking the program if the player has a particular letter and then searching the hand for how many if it does?

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Yeah that is exactly what this is doing. We're having the program check every tile that the player has in their hand.

let's say the player has 5 different characters that spell out 'apple'

if you pass in the arguement 'a' into this code, count returns one.

However if you pass in 'p' the count will return two.

You are challenge is asking you to do is loop through each tile that the player has in their hand, and return the number of tiles they have of a particular letter, if any.

Good job!