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

Eugen Oanta
Eugen Oanta
5,657 Points

Not sure what to do here

These are the requirements: 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!

Not sure how to write this code as it is not in any way related to the previous exercise...

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 int getTileCount(char tile) {
    int count = 0;
    for (char letter: mHand.toCharArray()) {
      if () {
      count++;
      }
  }
  return count;
}

3 Answers

You need another right brace at the end of file. If you click preview, it reads "reached end of file while parsing", which is typically a sign that your braces aren't together properly.

Please upvote and mark as best answer if you found this helpful!

Eugen Oanta
Eugen Oanta
5,657 Points

Hello! thanks for the reply, indeed I was missing a bracket, thanks for pointing that out, but still doesn't fix my problem. The idea is that I reach a result where the loop iterates through all the chars and returns 8, instead of 1. The error states that "the letter "e" was searched in an 8 letter string and instead of 1 instance of "e" the function returned 8. Basically the loop only reported the number of letters from the string. Hope someone resolved this exercise and can point me in the right direction.

Thank you.

Eugen Oanta
Eugen Oanta
5,657 Points

OK I got it :). For all the people struggling on this exercise this is the solution.

  1. To create a for loop which iterates through each char in the string is easy: ~~~ for(char x: mHand.toCharArray()) { } ~~~
  2. Then we need to make sure that each iteration of the loop matches the argument(tile) inserted in the method: ~~~ public int getTileCount(char tile) {

}

so the problem here is matching "tile" with "x". In order to do that, after the loop declaration we have to initialize a char variable which stores the value of x for each iteration. Afterwards becomes easy to use a conditional if to check either x equals the char variable , and if it does we will increment the count variable. So the code looks like this:

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

phewwww, I feel proud of myself :)), so easy but needed a lot of attention. I hope this kind of things will be given a much more attention, not just the proper syntax.