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

Niyamat Almass
Niyamat Almass
8,176 Points

i don't pass the challenge

I don't understand the challenge and where to start.i don't pass the challenge.please someone help me to pass the challenge

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;
  }
}
Lucas Santos
Lucas Santos
19,315 Points

Can you please post the question as well.

Niyamat Almass
Niyamat Almass
8,176 Points

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!

Niyamat Almass where is the piece of code that you tried but didn't pass? This is the same code as the challenge.

Niyamat Almass
Niyamat Almass
8,176 Points

Hey ,Gloria Dwomoh , can you tell me how to add my current challenge's workspace in comment .so that i can show you my problem

Niyamat Almass
Niyamat Almass
8,176 Points

Hey Gloria Dwomoh and Lucas Santos i am stuck in https://teamtreehouse.com/forum/i-have-a-problem-4 please solve the problem

Hi Niyamat Almass, in order to add the code you used for a challenge to your question....you click the "Ask a Question" button on the right next to the challenge you are currently trying to solve. Any changes you have done to the given code in you attempt to solve the problem will be attached with your question but if there are no changes in the given code i.e. you didn't really try to solve it, the given code will be attached as is. You can opt out your code from all that of course. I hope this helped.

Niyamat Almass
Niyamat Almass
8,176 Points

Hey! Gloria Dwomoh please solve the problem of this link https://teamtreehouse.com/forum/i-have-a-problem-4

2 Answers

Lucas Santos
Lucas Santos
19,315 Points

Here it the correct answer and why.

  public int getTileCount(char theTile){
  int count = 0;
    for( char tile : mHand.toCharArray()){
      if(tile == theTile){
    count++;
      }
    }
  return count;
  1. We make a method called getTileCount with a parameter of a certain character char theTile.

  2. we make a counter variable to keep track of how many tiles are in the variable mHand int count = 0;

  3. we use the foreach loop with char tile being the variable within the loop, meaning tile will be equal to mHand.toCharArray()

  4. mHand.toCharArray() get's the String stored in the mHand variable and makes each character in that string into an array of separate characters. So a string of Hello would be H, E, L, L, O

  5. we check to see if the parameter being passed in is equal to all the tiles in the hand. Because remember mHand.toCharArray() is now equal to tile

  6. if it is meaning that its in the hand increment count by 1 with count++

  7. AFTER THE LOOP return the count so we know how many tiles were in the hand.

Eleni Minadaki
Eleni Minadaki
3,687 Points

Thanks for your answer!