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 Data Structures Efficiency! Design the UI

How does the for in promptAction() works

Can someone please explain me how this for works?

  private String promptAction() throws IOException {
    System.out.printf("There are %d songs available. Your options are: %n", 
                      songBook.getSongCount());

    for (Map.Entry<String, String> option : menu.entrySet()) {
      System.out.printf("%s - %s %n",
                       option.getKey(),
                       option.getValue());      
    }
    System.out.print("What do you want to do: ");
    String choice = reader.readLine();
    return choice.trim().toLowerCase();
  }

1 Answer

RafaΕ‚ Sobczyk
RafaΕ‚ Sobczyk
3,274 Points

It iterates over every entry in out map, making it the value of variable "option" so we can use it later. If you don't know how the foreach loop - "for (Object o : objects)" works, watch the Feeling Loopy workshop here on TeamTreehouse. Basically, for every menu item in our menu map it prints the key and the value of that item.