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

melissakeith
3,766 PointsNeed help with getTileCount task
public int getTileCount() {
for (char tile: mHand.toCharArray()) {
char display = '-';
if (mHand.indexOf(tile)
This is what I added so far and don't know if I'm making it too complicated or what the next line should be?
5 Answers

Kourosh Raeen
23,733 PointsHi Melissa - You need to pass a letter to the method and then compare that letter with all the ones in mHands to find the count. So inside the loop you need an if statement to compare the variable tile with the letter passed in.
You also need an int variable to keep track of the count. Initialize it to zero outside the loop and then in your if statement increment it if tile is equal to letter.
Finally, outside the loop return the counter variable.

melissakeith
3,766 PointsIs there anywhere to find a reference for everything we're learning in Java and it's meaning? Its not enough to just rewatch videos, I see we're building on things we've already learned but there's no way I can remember how everything functions without having something to look over.

jcorum
71,830 PointsMelissa, the i is what is called a loop counter. It can be called anything: counter, loopCounter, j, k, whatever. Programmers usually use i because it's short and easy to type. This type of for loop has 3 parts inside the parentheses: a declaration and initialization of the loop counter; a condition that tells the loop when to stop; and a statement incrementing or decrementing the loop counter. It also has a code block -- the lines of code between the curly braces. The counter may or may not be used inside the code block. Here it is used to traverse the chars in mHand. w3schools/java/ has some good stuff on for loops, and Try It Yourself windows where you can practice. It's a great place to get the stuff firmly in mind. Not just for loops, but arrays, other control structures, etc.
Also, if you haven't done it already, and you may already be there and if so sorry, but if you haven't you should download an IDE like jGRASP of BlueJ (which are used in lots of schools) or Eclipse, which is more powerful and has a higher learning curve, and enter these programs there, compile them and run them. They are like having your own dedicated workspace. Workspaces are nice, but one very important thing they do not allow you to do is step through your programs step-by-step (in most IDEs it's called Debug mode), which sometimes is a much faster way to find a logic bug. And also a great way to learn how things really work.

Kourosh Raeen
23,733 PointsHi again Melissa - Personally, I would prefer to use the for each loop here, as suggested in the challenge. A regular for loop just unnecessarily complicates things and is prone to errors. It requires the int variable i, incrementing it, making sure that it doesn't go past the index of the last item in the array, using the length() function, and using the charAt() function. The code looks much cleaner with the enhanced for-loop:
public int getTileCount(char letter) {
int count = 0;
for (char tile: mHand.toCharArray()) {
if (tile == letter) {
count++;
}
}
return count;
}
As for using an IDE, I wouldn't rush into using one yet. Again, no need to complicate things while you're learning the basics, and that's exactly why Treehouse is using workspaces. If you are doing the Java Track here at Treehouse then after the Java Data Structures course you can learn about using an IDE in the Local Development Environments course and you'll get to use the IDE even more in the Build a JavaFX Application course and others.
And as for references, here are some options:
For a book, I recommend Head First Java: http://www.amazon.com/Head-First-Java-Kathy-Sierra/dp/0596009208/ref=sr_1_1?s=books&ie=UTF8&qid=1457187152&sr=1-1&keywords=head+first+java
Think Java is another book that is available for free: http://greenteapress.com/thinkjava6/thinkjava.pdf
And here's the link to Oracle's Java Tutorials: https://docs.oracle.com/javase/tutorial/
Best of luck and happy coding!

jcorum
71,830 Points public int getTileCount(char tile){
int mCount = 0;
for (int i = 0; i < mHand.length(); i++) {
if(mHand.charAt(i) == tile){
mCount++;
}
}
return mCount;
}

melissakeith
3,766 PointsWhat is the 'i' for?