Java Java Data Structures Exploring the Java Collection Framework Sets

Grigorij Schleifer
Grigorij Schleifer
10,352 Points

Can´t solve the Sets challenge

Here is my code in the Blog class:

 public Set<String> getAllAuthors(){
   Set<String> allPosts = new TreeSet<String>();
    for(String word: getPosts()){
      if(word.equals(mAuthor))
        allPosts.getAuthor(word);
    }
  }
    return allPosts;
}

Here ist the error:

./com/example/Blog.java:19: error: class, interface, or enum expected
 public Set getAllAuthors(){
        ^
./com/example/Blog.java:21: error: class, interface, or enum expected
    for(String word: getPosts()){
    ^
./com/example/Blog.java:24: error: class, interface, or enum expected
    }
    ^
./com/example/Blog.java:27: error: class, interface, or enum expected
}
^
Note: JavaTester.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
4 errors

What i am doing wrong????

Miroslav Kovac
Miroslav Kovac
11,344 Points

Hello, can you please share the whole code. I think that you are missing some bracers '{' or '}'. Or maybe your method is not in the class.

5 Answers

shot in the dark, just noticed it right off the bat...

 for(String word: getPosts()){
      if(word.equals(mAuthor)){  <--bracket
        allPosts.getAuthor(word);
      }  <--bracket
    }

Like Miroslav Kovac said would need more code to see more.

Grigorij Schleifer
Grigorij Schleifer
10,352 Points

Now I am here:

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;
  }
     public Set<String> getAllAuthors(){
   Set<String> allPosts = new TreeSet<String>();
    for(BlogPost word: getPosts()){
      if(word.equals(author)){
        allPosts.getAuthor(word);
    }
  }
    return allPosts;
 }
}

and get this error:

./com/example/Blog.java:20: error: cannot find symbol
      if(word.equals(author)){
                     ^
  symbol:   variable author
  location: class Blog
./com/example/Blog.java:21: error: cannot find symbol
        allPosts.getAuthor(word);
                ^
  symbol:   method getAuthor(BlogPost)
  location: variable allPosts of type Set
Note: JavaTester.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors

Why the compiler can´t find the author and the getAuthor method? Or am I on the wrong path ???

Not sure the other issue, but haven't done this course yet, but a treeset has no "getAuthor". But you might have set that somewhere else. Like I said haven't done this course so really another shot in the dark.

treeset

Grigorij Schleifer
Grigorij Schleifer
10,352 Points

This worked for me:

public Set<String> getAllAuthors(){  

Set<String> allPosts = new TreeSet<String>();     
for(BlogPost word: getPosts()){       
String postAuthors = word.getAuthor();
allPosts.add(postAuthors);     
}   
}
return allPosts;       
}

it does not for me :-(

There looks to be an extra closing brace before your return statement.

ryanjones6
ryanjones6
13,797 Points

<p>Grigorij Schleifer example is on the mark except for the extra ending curly bracket before the return statment.</p>

public Set<String> getAllAuthors(){
// <Create a new temporary ordered Set that contains the type String>
   Set<String> allPosts = new TreeSet<String>();

// <iterate through each BlogPost>
   for(BlogPost word: getPosts()){

// <adds author from each BlogPost to the temporary ordered Set: allPosts>  
       allPosts.add(word.getAuthor());
   }
// <returns all post authors as an ordered set>
   return allPosts;
}
ryanjones6
ryanjones6
13,797 Points

Thanks for the responses from everyone. I kind of forgot about this post.