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

Michael Krarup
Michael Krarup
1,189 Points

required: array or java.lang.Iterable

Hello fellow students!

I just encountered this strange error in my program that i have been struggling with. For some reason the for loop doesn't review every char instance in my string and doesn't return the number of instances but only the first occurence. Am i using a wrong method?

Error from latest try at this challenge:

./ScrabblePlayer.java:23: error: for-each not applicable to expression type for (char letter1 : tiles.indexOf(letter)) { ^ required: array or java.lang.Iterable found: int Note: JavaTester.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error

ScrabblePlayer.java
public class ScrabblePlayer {
  // A String representing all of the tiles that this player has
  private String tiles;
  int count = 0; 

  public ScrabblePlayer() {
    tiles = "";
  }

  public String getTiles() {
    return tiles;
  }

  public void addTile(char tile) {
    tiles += tile;
  }

  public boolean hasTile(char tile) {
    return tiles.indexOf(tile) != -1;
  }

  int getCountOfLetter(char letter) {
  for (char letter1 : tiles.indexOf(letter)) {
 if (letter1 == letter) {
              count++; 
          }
          return count;
      }
    }

}

1 Answer

Hi Michael,

Yes, you've not selected the right method for your for loop. Break the tiles string down into characters by using toCharArray() instead. The indexOf() method returns the position the char is within the string. We want to iterate over the stringso we need an iterable object - that's what your error is saying. Using toCharArray() converts a string to an iterable array that your for loop can pass over one char at a time.

[EDIT] You also need to declare count prior to your for loop (sorry - I just noticed you've done this at class level, not at method level - you can move it inside the method as it isn't required for each instance of the class). Next, move the return statement - it is inside the for loop. You want to return count after the loop. Just move it one brace lower.

Let me know how you get on.

Steve.

Michael Krarup
Michael Krarup
1,189 Points

Hello Steve :-)

I appreciate you taking the time to cut it out for me as i would have otherwise tirelessly tried different inputs. I hope that i don't give off the impression that i just want the answer and be quickly done, because i don't. I really wanted to know how the method worked and how i combined it with the for-loop. Needless to say that your advice was greatly appreciated and in fact got me to understand the construction of the loop (+ some additional syntax faults that might appear!). Thank you so much and have a wonderful weekend!

Kind regards, Michael.

Hi Michae;,

Thanks for getting back to me -- I'm happy that you were able to complete the code challenge. Good work!

Have a great weekend yourself!

Steve.