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 Exploring the Java Collection Framework Maps

Andrew D.
Andrew D.
2,937 Points

Code Exactly Like Craig's, But Not Working

Hey guys.. my code is exactly the same as Craig's, but I keep getting "<identifier> expected" errors. My research indicates that it might be due to improper importing, but I imported both java.util.Map and java.util.HashMap.

Here's the code:

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);
    }
  }
  System.out.printf("Hash tag counts: %s %n", hashTagCounts);
}

It would help a lot if you put the exact errors you were getting in your question. It's a bit hard to decipher what your problem is, otherwise.

Andrew D.
Andrew D.
2,937 Points

Sure, good idea karis:

Example.java:30: error: <identifier> expected
for (Treet treet : treets) {
^
Example.java:40: error: <identifier> expected
System.out.printf("Hash tag counts: %s %n", hashTagCounts);
^
Example.java:40: error: illegal start of type
System.out.printf("Hash tag counts: %s %n", hashTagCounts);
^
Example.java:40: error: <identifier> expected
System.out.printf("Hash tag counts: %s %n", hashTagCounts);

Also.. how does one paste the code all beautifully n stuff?

If you look right below the text input box, there's a line that says: "Reference this Markdown Cheatsheet for syntax examples for formatting your post." Just click the hyperlink to see all the formatting characters you can use. Working on your problem; will post soon.

5 Answers

Christopher Augg
Christopher Augg
21,223 Points

Andrew,

Thanks for the link. I am not sure if you are aware, but you can take a snapshot of your actual workspace. When you have your workspace open, look at the top right side of it. There are 3 icons from which to choose. A snapshot icon, fork icon, and a preview icon. You can click the snapshot icon to make a snapshot and get a link to use here. I just thought you might like to know considering it is easier.

As for your code, you have to love those those braces! Your Example class has braces in the wrong places - no, I did not intend on rhyming.

       public class Example { 

       public static void main(String[] args) {
           Treet[] treets = Treets.load();
           System.out.printf("There are %d treets. %n",
                     treets.length);
           Set<String> allHashTags = new HashSet<String>();
           Set<String> allMentions = new TreeSet<String>();
           for (Treet treet : treets) {
              allHashTags.addAll(treet.getHashTags());
              allMentions.addAll(treet.getMentions());
           }
           System.out.printf("Hash tags: %s %n", allHashTags);
           System.out.printf("Mentions: %s %n", allMentions);
           //removed the } from this line
           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);
          }
        }
        System.out.printf("Hash tag counts: %s %n", hashTagCounts);
      }
     } //added the brace here 

Hope this helps.

Regards,

Chris

Andrew D.
Andrew D.
2,937 Points

Thank you, Chris!

Christopher Augg
Christopher Augg
21,223 Points

Andrew,

Looking at your code the Map is not the same as provided by Craig in the video

       Map hashTagCounts = new HashMap();  //should be

       Map<String, Integer> hashTagCounts = new HashMap<String, Integer>();

Please take a snapshot of your work space and provide a link if this does not fix the issue and we can take a look at it further.

Regards,

Chris

I don't see where you initialized the count variable. Is that somewhere else in your code? If so, I haven't done this exercise, so context for this block would help me help you.

Andrew D.
Andrew D.
2,937 Points

Strange, Christopher and karis. When I pasted the code, it didn't paste the whole thing for whatever reason. Here's the original code that's still not working, in proper coding syntax:

  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);
    }
  }
  System.out.printf("Hash tag counts: %s %n", hashTagCounts);
}
Christopher Augg
Christopher Augg
21,223 Points

No problem Andrew. Please take a snapshot of your workspace and provide the link here. This will allow me to fork it and test all of your code.

Andrew D.
Andrew D.
2,937 Points

Here you go, Christopher. Thank you for your help :-)

http://pastebin.com/Vid5SxXp

Andre Colares
Andre Colares
5,437 Points
            import com.andrecolares.Treet;
import com.andrecolares.Treets;


import java.util.*;

public class Example {

    public static void main(String[] args) {
        Treet[] treets = Treets.load();
        System.out.printf("There are %d treets. %n", treets.length);
        Set<String> allHashTags = new HashSet<String>();
        Set<String> allMentions = new TreeSet<String>();
        for (Treet treet : treets) {
            allHashTags.addAll(treet.getHashTags());
            allMentions.addAll(treet.getMentions());
        }
        System.out.printf("Hash tags: %s %n", allHashTags);
        System.out.printf("Mentions: %s %n", allMentions);

        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);
            }
        }
        System.out.printf("Hash tag counts: %s %n ", hashTagCounts);

        Map<String, List<Treet>> treetsByAuthor = new HashMap<String, List<Treet>>();
        for (Treet treet : treets) {
            List<Treet> authoredTreets = treetsByAuthor.get(treet.getAuthor());
            if (authoredTreets == null) {
                authoredTreets = new ArrayList<Treet>();
                treetsByAuthor.put(treet.getAuthor(), authoredTreets);
            }
            authoredTreets.add(treet);
        }
        System.out.printf("Treets by author: %s %n ", treetsByAuthor);
        System.out.printf("Treets by nickrp: %s %n ", treetsByAuthor.get("nickrp"));

    }
}
            ```

This is working.