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

Sohaib Rashid
Sohaib Rashid
627 Points

I used the same format as the prior video, but I am still stuck

If someone can assist, it would help me a lot! Happy coding!

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 getTitleCount()
     String progress;
    for (char tile: mHand.toCharArray()) {
      char display = "-";
      if (mHand.indexof(tile) >= 0) {
        display = tile;
      }
      progress += display;
      return progress;
  }
}

1 Answer

Rebecca Bompiani
Rebecca Bompiani
16,996 Points

Hi Sohaib-

The basic logic for the challenge is as follows:

"For each char in mHand, if the tile matches the char, add one to the counter"

Try:

 public int getTileCount(char tile) {       //this method should return an int, because it will give you the number of times a letter shows up
    int counter = 0;       // initialize the counter at 0
    for (char letter : mHand.toCharArray()) {       //push all the characters in mHand to an array of characters, letter
      if (letter==tile) {      //loop through the chars, checking to see if the chars from the hand match the selected letter
        counter ++;       //if the char matches the letter selected, add one to the count
      }
    } 
    return counter;  //return the count
  }
Sohaib Rashid
Sohaib Rashid
627 Points

You simplified it so much! Thank you for your help, i sure hope I can simplify and think as intelligent as you do! Happy Coding!!!!