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 trialDinu Comendant
6,049 PointsMaps Code Challenge: for-each not applicable to expression type
it tells me "for-each not applicable to expression type" and it points at "for(String category : post.getCategory())" I have no clue how to solve it
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<BlogPost, Integer> getCategoryCounts () {
Map<String, Integer> categoriesCount = new HashMap<String, Integer>();
for (BlogPost post : mPosts) {
for(String category : post.getCategory()) {
Integer count = categoriesCount.get(category);
if (count == null) {
count = 0;
}
count ++;
categoriesCount.put(category, count);
}
}
return categoriesCount;
}
}
3 Answers
Emmanuel C
10,636 PointsHey Dinu,
getCategory returns a String, and youre not allowed to iterate through a string, unless you call toCharArray() on it, which would allow you to iterate through each character in the string. However i dont believe thats what youre trying to accomplish.
If a BlogPost is supposed to have multiple categories, then I would suggest having a list of category in the class, and use that to iterate through them. Also List have a method called size() to get the number of items in the list.
I hope that helps, if not feel free to post some comments. Good Luck.
Steven Parker
231,269 PointsYou're pretty close here, I see only two issues:
The "getCategory" method returns the category as a string, just assign it to "category" directly (no loop needed).
And the method itself is defined as returning a "Map<BlogPost, Integer>", but the type of the object being returned is actually "Map<String, Integer>" — these need to match.
Fix those and you should pass the challenge!
Dinu Comendant
6,049 PointsThanks a lot Steven and Emmanuel! I really appreciate your swift answers! I went through! I don't feel that I grasped the whole problem with the getCategory though. I'm wondering why is it in the video the method works, and in the challenge it doesn't?
Map<String, Integer> hashTagCounts = new HashMap<String, Integer>();
for (Treet treet : treets) {
for (String hashTag : treet.getHashTags()) {
Integer count = hashTagCounts.get(hashTag);
if (count == null) {
count = 0;
}
count++;
hashTagCounts.put(hashTag, count);
}
Steven Parker
231,269 PointsThese examples are working with different structures. The "getHashTags" method returns an iterable collection (suitable for a loop), but "getCategory" returns a String.