Java Java Data Structures Exploring the Java Collection Framework Sets

Thomas Williams
Thomas Williams
9,090 Points

Illegal start of 'for'??

package com.example;

import java.util.List; import java.util.Set; import java.util.TreeSet;

public class Blog { List<BlogPost> mPosts;

public Blog(List<BlogPost> posts) {

mPosts = posts;

}

public List<BlogPost> getPosts() { return mPosts; } Set<String> getAllAuthors = new TreeSet<String>(); for (BlogPost post : Blog) { getAllAuthors.addAll(post.getAuthor); }

}

I'm not sure whats wrong with this but i'm getting illegal start of the 'for' loop?? Please help!

3 Answers

Hello,

It looks like some of your code is not inside any functions.

Set getAllAuthors = new TreeSet(); for (BlogPost post : Blog) { getAllAuthors.addAll(post.getAuthor); }

is the code I'm referring to. It looks like some of it might have gotten mistakenly deleted.

Thomas Williams
Thomas Williams
9,090 Points

Haha, that is the only bit of code I have written, the only bit of code missing is String after Set and TreeSet, it hasn't pasted for some reason. The challenge was to add a method to this Blog class that takes the author from each blogpost and puts them into an alphabetically ordered list using the java.util.Set interface. The getAuthor method is in the BlogPost class. To my mind this code is saying create a new alphabetically ordered list called getAllAuthors, then for each blogpost in Blog (a list of blogposts) get the author and add it to the getAllAuthors list, hence, completing the challenge. Clearly my understanding isn't quite what it should be yet!

Hello,

You want getAllAuthors to be a method of the Blog class. Then inside that method, you want to do all of the logic for creating the set, adding all of the authors to it, and return the set.

Did you complete this challenge? I've stuck with it.

public Set<String> getAllAuthors() { Set<String> allAuthors = new TreeSet<String>(); for (??? ???: ???) { allAuthors.addAll(???.getAuthor()); } return allAuthors;

Need something like that?

Its close, you need to make sure you define what the sets are holding like

Set<SomeType> someSet = new TreeSet<>();

as well as in your returntype. For the for loop, consider what you're looping over, you need to define it as

for(Type variable: someList)

Got this:

public Set<String> getAllAuthors() {
    Set<String> allAuthors = new TreeSet<String>();
    for (Blog post: posts) {
      allAuthors.addAll(post.getAuthor());
    }
     return allAuthors;

But an error occured:

./com/example/Blog.java:21: error: cannot find symbol
    for (Blog post: posts) {
                    ^
  symbol:   variable posts
  location: class Blog
./com/example/Blog.java:22: error: cannot find symbol
      allAuthors.addAll(post.getAuthor());
                            ^
  symbol:   method getAuthor()
  location: variable post of type Blog
Note: JavaTester.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors

First, the posts are in the member variable mPosts. Second, you might want to use Set.add instead of Set.addAll since you're only adding a single item and not a collection.

I did it, man! Thank you very much!

Juan Santiago
Juan Santiago
3,766 Points

This is my code, I'm stuck with the compiler errors (I've tried a lot of combinations with the "for" statement and the .getAuthor variable) but I can't figure it out why :/ HELP!

public Set<String> getAllAuthors() { Set<String> allAuthors = new TreeSet<String>(); for (Blog post:posts) { allAuthors.add(post.getAuthor()); } return allAuthors; }

Juan Santiago
Juan Santiago
3,766 Points

I solve it :3

 public Set<String> getAllAuthors() {
    Set<String> allAuthors = new TreeSet<String>();
    for (BlogPost post: mPosts) {
      allAuthors.add(post.getAuthor());
    }
     return allAuthors;
}