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

(Maps - Java) I have followed the exactly how Craig has shown us but it still won't compile. Can anyone please help me?

The following contains my error message :

Example.java:45: error: illegal start of type                                                                                       
     for (Treet treet : treets) {                                                                                                   
     ^                                                                                                                              
Example.java:45: error: ')' expected                                                                                                
     for (Treet treet : treets) {                                                                                                   
                     ^                                                                                                              
Example.java:45: error: <identifier> expected                                                                                       
     for (Treet treet : treets) {                                                                                                   
                              ^                                                                                                     
Example.java:53: error: <identifier> expected                                                                                       
      System.out.printf("Treets by author : %s %n", treetsByAuthor);                                                                
                       ^                                                                                                            
Example.java:53: error: illegal start of type                                                                                       
      System.out.printf("Treets by author : %s %n", treetsByAuthor);                                                                
                        ^                                                                                                           
Example.java:53: error: <identifier> expected                                                                                       
      System.out.printf("Treets by author : %s %n", treetsByAuthor);                                                                
                                                                  ^                                                                 
Example.java:54: error: <identifier> expected                                                                                       
      System.out.printf("Treets by nickrp : %s %n", treetsByAuthor.get("nickrp"));                                                  
                       ^                                                                                                            
Example.java:54: error: illegal start of type                                                                                       
      System.out.printf("Treets by nickrp : %s %n", treetsByAuthor.get("nickrp"));                                                  
                        ^                                                                                                           
Example.java:54: error: <identifier> expected
      System.out.printf("Treets by nickrp : %s %n", treetsByAuthor.get("nickrp"));                                                  
                                                                      ^                                                             
Example.java:54: error: ';' expected                                                                                                
      System.out.printf("Treets by nickrp : %s %n", treetsByAuthor.get("nickrp"));                                                  
                                                                       ^                                                            
Example.java:54: error: illegal start of type                                                                                       
      System.out.printf("Treets by nickrp : %s %n", treetsByAuthor.get("nickrp"));                                                  
                                                                               ^                                                    
Example.java:54: error: <identifier> expected                                                                                       
      System.out.printf("Treets by nickrp : %s %n", treetsByAuthor.get("nickrp"));                                                  
                                                                                ^                                                   
12 errors                

The following is the class Example, where all the problems are occurring.

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Date;
import java.util.Set;
import java.util.List;
import java.util.Map;
import java.util.HashSet;
import java.util.HashMap;
import java.util.TreeSet;

import com.teamtreehouse.Treet;
import com.teamtreehouse.Treets;


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 TreeSet<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);
    }
 //----------THE LINES STARTING FROM HERE CONTAIN THE ERRORS---------------- 
    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"));
}

I don't know if it runs on other machines but it isn't working on my workspace.

1 Answer

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

The closing brace just above the line with LINES STARTING ... closes public static void main, that is why code after that closing brace is not right:

    System.out.printf("Hash tag counts : %s %n",hashTagCounts);
    // } remove this brace and put it to the very end
 //----------THE LINES STARTING FROM HERE CONTAIN THE ERRORS---------------- 

Also add closing brace at the very end:

package com.teamtreehouse;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Date;
import java.util.Set;
import java.util.List;
import java.util.Map;
import java.util.HashSet;
import java.util.HashMap;
import java.util.TreeSet;

import com.teamtreehouse.Treet;
import com.teamtreehouse.Treets;


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 TreeSet<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);
        //----------THE LINES STARTING FROM HERE CONTAIN THE ERRORS---------------- 
        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"));
    }
}

It was such a silly mistake of mine, thank you for pointing it out Alexander Nikiforov.