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 Creating the MVP Counting Scrabble Tiles

Sarabeth Zogg
Sarabeth Zogg
9,418 Points

Enhanced For Loop on Character Array

INSTRUCTIONS: Now in your new method, have it return a number representing the count of tiles that match the letter that was passed in to the method.

MY CODE: private String tiles; public int getCountOfLetter (char letter){ int count = 0; char[] stringToCharArray = tiles.toCharArray(); for (char output : stringToCharArray) { if (output == letter){ count++; } } return count; }

ERROR: Bummer! Please use an enhanced for loop to cycle through each of the tiles found in tiles.toCharArray.

...WHICH IS EXACTLY WHAT I AM DOING...? THANKS IN ADVANCE FOR HELP!

1 Answer

Simon Coates
Simon Coates
28,694 Points

verification can be weird. It passed for me using:

  public int getCountOfLetter (char letter){
    int count = 0; 
    //char[] stringToCharArray = tiles.toCharArray();
    for (char output : tiles.toCharArray() ) {
      if (output == letter){
        count++; 
      }
    } 
    return count; 
  }
Sarabeth Zogg
Sarabeth Zogg
9,418 Points

Thanks. I was trying it the way you commented out also...I think that would work too.