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 trialiclick
10,945 PointsHaving problem with the error code. Cant find it.
./com/example/Blog.java:32: error: for-each not applicable to expression type for (String category : bp.getCategory() ) { ^ required: array or java.lang.Iterable found: String Note: JavaTester.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error
4 Answers
Seth Kroger
56,413 PointsIt you help to see you code but here's something to consider. Does a BlogPost have multiple categories or only one? You only need a for-each loop if it's multiple. If it's only one you just need getCategory().
Seth Kroger
56,413 PointsIt looks like you're almost there. You're just missing a return categoryCounts; statement and extra curly brace after categoryCounts.put(category, count);
iclick
10,945 PointsI did add the category and the missing curly is the one i'm having trouble with but it is pointing on the very last curly
iclick
10,945 Pointssame error Seth :(
public Map<String, Integer> getCategoryCounts() { Map<String, Integer> categoryCounts = new HashMap<String, Integer>(); for (BlogPost post : mPosts) { String category = post.getCategory(); Integer count = categoryCounts.get(category); { if (count == null) { count = 0; } count++; categoryCounts.put(category, count); } return categoryCounts; { }
Seth Kroger
56,413 PointsI only meant the closing brace, remove the opening brace on return's line and it should be good.
iclick
10,945 PointsI'm still having error message Seth :(
public Map<String, Integer> getCategoryCounts() { Map<String, Integer> categoryCounts = new HashMap<String, Integer>(); for (BlogPost post : mPosts) { String category = post.getCategory(); Integer count = categoryCounts.get(category); { if (count == null) { count = 0; } count++; categoryCounts.put(category, count); } return categoryCounts;
Error:
./com/example/Blog.java:40: error: reached end of file while parsing return categoryCounts; ^ Note: JavaTester.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error
iclick
10,945 PointsI got it Seth. Thanks for the response :)
iclick
10,945 PointsI got it Seth. Thanks for the response :)
iclick
10,945 Pointsiclick
10,945 PointsSeth here's the code:
public Map<String, Integer> getCategoryCounts() { Map<String, Integer> categoryCounts = new HashMap<String, Integer>(); for (BlogPost post : mPosts) { String category = post.getCategory(); Integer count = categoryCounts.get(category); if (count == null) { count = 0; } count++; categoryCounts.put(category, count); }
Error Message:
./com/example/Blog.java:39: error: reached end of file while parsing } ^ Note: JavaTester.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error