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 trialEric Shelby
28,300 PointsMapping issues
I am having an issuing involving Maps. The error that keeps coming up is in the second for loop. It states "for-each not applicable to expression type." It apparently needs an array entry, but I don't know how to add that. I don't have any methods that go with that either. A hint or two would be appreciated.
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.Map;
import java.util.HashMap;
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;
}
public Map<String, Integer> getCategoryCounts() {
Map<String, Integer> categories = new HashMap<String, Integer>();
for (BlogPost post: mPosts) {
for (String category : post.getCategory()) {
Integer count = categories.get(post);
if (count == null) {
count = 0;
}
count++;
categories.put(category, count);
}
}
return categories;
}
}
4 Answers
Alex Bratkovskij
5,329 PointsOkay, the issue is, your
java post.getCategory()
returns a string, The compiler expects to find an ARRAY of strings, not A string :)
So to complete the task, you need to provide a code that looks like:
For each blogPost set count; check if the value already in a map, if not, set key to 0; get category; add it to Map; increment count;
You don't need a nested for loop for this task )))
if you need more assistance with the code itself:
public Map<String, Integer> getCategoryCounts(){
Map<String, Integer> getCategoryCounts = new HashMap<String, Integer>();
for (BlogPost post : mPosts){
if(getCategoryCounts.get(post.getCategory()) == null){
getCategoryCounts.put(post.getCategory(), 0);
}
int count = getCategoryCounts.get(post.getCategory());
count++;
getCategoryCounts.put(post.getCategory(), count);
}
return getCategoryCounts;
}
Alex Bratkovskij
5,329 PointsBecause Lists are not in proper sequence, what you want is to convert your list into array.
here is the method: https://docs.oracle.com/javase/7/docs/api/java/util/List.html#toArray()
Eric Shelby
28,300 PointsI think using toArray() is the right answer, but I don't know where to put it. I tried putting it in the first for loop next to mPosts, but the compiler stated that "Object cannot be converted to BlogPost." Where in the code do I convert the list into an array?
Eric Shelby
28,300 PointsThank you very much. I finally completed the challenge. This was such a complicated effort that I'm not sure I could have done this on my own.