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

Can you please add a method called getTileCount that uses the for each loop you just learned to loop through the charac

Having problems - I think I got the general idea but some minor bugs, I was able to implement the for loop

for(char letter: mHand.toCharArray()){
   }

the compiler throws an error stating my if statement is accessing a char when it is supposed to access a string.

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }

  public String getTileCount() {
     int count = 0;
     for(char letter: mHand.toCharArray()){
       if (letter.contains(mHand)){
         count += 1;
         System.out.println("there are " +count+ "number of"+letter+" letters");
       }

     }
  }


  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;
  }
}

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

It is supposed to access a character. Because your function is supposed to accept a character. We're looking for how many of a certain character is currently in your hand. But currently, nothing is being passed into your function.

I posted a fairly detailed explanation not terribly long ago along with a solution if the above hint wasn't enough. You can view it here:

https://teamtreehouse.com/community/help-to-solve-code-challenge-also-please-explain-the-logic-and-thanks-in-advance

Good luck! :smile:

wow you have 25,000 points lol, and all since febuary. tag ..your IT!

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Thomas Katalenas hahaha well not quite. 24.5k :smile: I'm working on it though! Was working on the Swift 2.0 track, but my Mac mini died a few weeks ago, so have switched over to Java :thumbsup: