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 trialMichael C
3,230 PointsChallenge Exploring JCF Bummer!
Actually I'm pretty sure I was right. But the Bummer tells me that he expects a "1" instead of the "6" I returned. I don't really know why this happens since I can't see how many mPosts are there at all and on which index the "Entertainment" Category appears.
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 java.util.HashMap;
import java.util.Map;
public class Blog {
List<BlogPost> mPosts;
public Blog(List<BlogPost> posts) {
mPosts = posts;
}
public Map<String, Integer> getCategoryCounts()
{
Integer count = 0;
Map<String, Integer> map = new HashMap<String, Integer>();
for(BlogPost item : mPosts)
{
map.put(item.getCategory(), count++);
}
return map;
}
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;
}
}
2 Answers
Craig Dennis
Treehouse TeacherEach BlogPost
can only be in one category, so this is an overview of how many BlogPost
s are in each category. So its key is the category and the value is the count of how many posts are in that category.
Make sense?
Michael C
3,230 PointsThanks chief,
Now I understood the challenge!
Craig Dennis
Treehouse TeacherHmmm... do you remember the lesson in Java Objects (around 2:00 minutes in) where I talked about the dangers of post-incrementing (variable++)?
Also...it seems like you are not pulling the count of the map for the category, just every time putting in a global count at that time.
I do a similar loop in the Maps video around 8:30, about counting Treets by hashtag.
Hope it helps!
Michael C
3,230 PointsOne Questions. Does this challenge wants me to store the Category with their index number?
like
map.get(Category1) // returns 1
map.get(Category2) // returns 2 and so forth for every new added category
or the occurence of a category in a List<BlogPost> mPost object?
like
map.get(Category1) // returns 5 (since Category1 appears 5 times in the List)
map.get(Category2) // returns 1 (since Category2 appears 1 time in the List)
Michael C
3,230 PointsMichael C
3,230 PointsgetCategoryCounts is the method which creates the problem.