Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Creating the MVP For Each Loop

Mark Miller
Mark Miller
45,831 Points

Counter must be initialized outside of this method or what?

It would be easy to just initialize counter outside of this method, and then you could just invoke the method whenever you have a match in the game, to increment counter. I see no need nor any way to utilize a for-each loop here. And I can't see how to keep counter inside the scope of the method because it must be initialized somewhere, but doing that inside of the method will reset its value every time you pass in a char. So counter will never add upwards unless you declare it outside of the method. And why would I need this for-each loop just to increment counter? Are we just passing in a char? I don't understand this quiz question.

1 Answer

Seth Kroger
Seth Kroger
56,407 Points

The challenge is asking you to write a method that answers the question "How many letters [blank] do I have?" (ie. "How many E's do I have?", or "How many Z's?", etc.) Keeping a persistent count outside the method isn't helpful because your Scrabble hand changes throughout the game as you play, and your method doesn't know ahead of time what letter it will be asked about. Instead you declare it inside the method as a local variable. The count should be calculated afresh every time. It should look something like this:

public int getTileCount(char tile) {
   int tileCount = 0;
   // code to count the tiles goes here...
   return tileCount;
}