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

loop for each Map Entry in Java

I'm having trouble trying to replicate the promptAction() method in the KaraokeMachine. I'm getting an error that says:

./com/teamtreehouse/KaraokeMachine.java:27: error: incompatible types: Object cannot be converted to Entry<String,String>
for (Map.Entry<String, String> option : mMenu.entrySet()) {

Apparently there's a type casting issue.

Here's my full method:

  private String promptAction() throws IOException {
    System.out.printf("There are %d songs available. Your options are: %n", 
                      mSongBook.getSongCount());
    for (Map.Entry<String, String> option : mMenu.entrySet()) {
      System.out.printf("%s - %s",
                        option.getKey(),
                        option.getValue());
    }
    System.out.println("What do you want to do:  ");
    String choice = mReader.readLine();
    return choice.trim().toLowerCase();
  }

1 Answer

I fixed my problem. It was with my Map member declaration.

I had this: private Map mMenu;

instead of this: private Map<String, String> mMenu;

The error message was fooling me and making me stare at the wrong line of code.