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

Cannot figure out why I get an error: "for-each not applicable to expression type".

I searched up examples and they show:

public void createFile(String... fn) {
    for (String f : fn) {
        ...

So I expected this to work:

public Map<String, Video> videosByTitle(Course course) {
    Map<String, Video> videoMap = new HashMap<>();
    List<Video> videoList = course.getVideos();
    for (Course c : course) {
        System.out.println("%s", c.getName()); 
    }/*
    for (Video video : videoList) {
      System.out.printf("%s - %s%n", course.getName(), video.getTitle());
        videoMap.put(course.getName(), video);
    }*/
    return videoMap;
  }

Can someone give me some tips and/or explanations that may not be clear to me? Thanks~

1 Answer

AFJNIK hassan
AFJNIK hassan
11,416 Points

hi you try to get name with course's method , but what is expected is the title of video witch is in video class

public Map<String, Video> videosByTitle(Course course) {

  Map<String, Video> videoMap = new HashMap<String, Video>();
  List<Video> videoList = course.getVideos();

  for (Video v : videoList) {
    videoMap.put(v.getTitle(), v);
  }

  return videoMap
}