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

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