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 trialDavid Jones
1,681 PointsHelp with set method code challenge.
I am getting errors when I enter the code bellow. I am not sure how to add the name of the authors to the set allAuthors.
package com.example;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class BlogPost implements Comparable<BlogPost>, Serializable {
private String mAuthor;
private String mTitle;
private String mBody;
private String mCategory;
private Date mCreationDate;
public BlogPost(String author, String title, String body, String category, Date creationDate) {
mAuthor = author;
mTitle = title;
mBody = body;
mCategory = category;
mCreationDate = creationDate;
}
public int compareTo(BlogPost other) {
if (equals(other)) {
return 0;
}
return mCreationDate.compareTo(other.mCreationDate);
}
public String[] getWords() {
return mBody.split("\\s+");
}
public List<String> getExternalLinks() {
List<String> links = new ArrayList<String>();
for (String word : getWords()) {
if (word.startsWith("http")) {
links.add(word);
}
}
return links;
}
public String getAuthor() {
return mAuthor;
}
public String getTitle() {
return mTitle;
}
public String getBody() {
return mBody;
}
public String getCategory() {
return mCategory;
}
public Date getCreationDate() {
return mCreationDate;
}
}
package com.example;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import com.example.BlogPost;
public class Blog {
List<BlogPost> mPosts;
public Blog(List<BlogPost> posts) {
mPosts = posts;
}
public Set<String> getAllAuthors (){
Set<String> allAuthors = new TreeSet<String>();
for( BlogPost author : Blog.getPosts()) {
allAuthors.addAll(BlogPost.getAuthor());
}
return allAuthors;
}
public static List<BlogPost> getPosts() {
return mPosts;
}
}
2 Answers
Jeremy Hill
29,567 PointsYour for loop should loop through mPosts and through each iteration you need to add .getAuthor() to the set "allAuthors" and then after the loop ends return your set "allAuthors". Make sure that you import Set and TreeSet.
If you don't understand here is my code:
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> authors = new TreeSet<>();
for(BlogPost post : mPosts){
authors.add(post.getAuthor());
}
return authors;
}
}
Jeremy Hill
29,567 PointsSince class Blog is the class that you are working in you would be better off doing this.getPosts(), but the best way to do it is directly through mPosts because your constructor's job is to initialize the private variable with the information that you need when the class is instantiated.
David Jones
1,681 PointsDavid Jones
1,681 PointsThank you so much. I had a few misconceptions and this really cleared up a lot. One was I was using the method getPosts() instead of just mPosts in the for each loop. I kept getting a error message saying getPosts() was not a static method. Then after I made getPosts() static I got an error message saying mPosts could not be used because it was not static.
So the lesson is while in the class always use the private field instead of the method used to call it in other classes. But can you explain why making getPosts() static and using Blog.getPosts() does not work?
The critical mistake I made was looking for String instead of a BlogPost in the for each loop. I see now that mPost is made up of a list of BlogPosts so it makes sense to look for them instead of Strings. Thanks again for the help!!