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

Alexander Nowak
Alexander Nowak
498 Points

for each loop challage

Hi

Think iv almost got it but could someone please pint out where I'm going wrong?

Thanks

I can't seem to attach my code. How do I do this?

Thanks

1 Answer

Kevin Faust
Kevin Faust
15,353 Points

Hey Alexander,

The completed code looks like this

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

So first we create the method that accepts a char. we then initialize a counter variable that starts at 0. Now we're going to convert mHands to an array of chars so we can loop through each char. Each time we loop through a mHand char, we store it as tiles. So now if mHands consisted of "asdfu" for example, it would now look like this ["a","s","d","f","u"]. Its a array of chars!. and now we loop through each one. so in the first loop, "tiles" would hold the letter "a" and then so on. We check if the tile we're looping on is equal to the one we passed in. If they match, then increase the counter by 1. Once we loop thorugh all the chars and check if it equals the one we pass in, we return the count.

Does that make sense?

Kevin