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 trial

Java Java Data Structures - Retired Exploring the Java Collection Framework Maps

Having 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
Seth Kroger
56,413 Points

It 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 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

Seth Kroger
Seth Kroger
56,413 Points

It looks like you're almost there. You're just missing a return categoryCounts; statement and extra curly brace after categoryCounts.put(category, count);

I 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

same 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
Seth Kroger
56,413 Points

I only meant the closing brace, remove the opening brace on return's line and it should be good.

I'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

I got it Seth. Thanks for the response :)

I got it Seth. Thanks for the response :)