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 Using ArrayLists

Denis Madroane
Denis Madroane
2,983 Points

The Console returns a full list of tweets instead of a hashtag list. What am I doing wrong?

So I followed in Craig's steps closely, even checked for typos a few times. I get no errors whatsoever but instead of the expected output "There are 58 Tweets. Hashtags: #treet" I got a full list of all 58 tweets. Here's some of it:

Picked up JAVA_TOOL_OPTIONS: -Xmx128m                                            
Picked up _JAVA_OPTIONS: -Xmx128m                                                
There are 58 treets.                                                             
Treet:  "Want to be famous? Simply tweet about Java and use the hashtag #treet. I
’ll use your tweet in a new @treehouse course about data structures." by craigsde
nnis on Wed Jan 21 22:12:32 UTC 2015                                             
Treet:  "@treehouse makes learning Java sooooo fun! #treet" by journeytocode on W
ed Jan 21 22:19:27 UTC 2015                                                      
Treet:  "I want to be famous on @treehouse with Java and  #treet" by DJPechi on W
ed Jan 21 22:26:22 UTC 2015                                                      
Treet:  "@kaiuhl Thanks, what a #treet.  You need to mention Java too.  That’s wh
at I really want." by craigsdennis on Wed Jan 21 22:32:12 UTC 2015               
Treet:  "The golden rule in #java isn't #treet others as you want to be treated. 
The golden rule is don't repeat yourself." by FrankLamar_Dev on Wed Jan 21 22:33:
\\And it goessss onnnn....

Even though I checked for errors several times, I can't figure out what I did wrong. I surely would appreciate it if someone would clear this out for me. The strange thing is I couldn't find any line containing "Hashtags:" in the console output. It's like its ignoring my lines about sorting the words containing hashtags. Oh, and it's not the case of me forgetting to hit Save, I made sure I did that. Thanks! Here's my code:

Treet.java

      package com.teamtreehouse;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class Treet implements Comparable, Serializable {
  private static final long serialVersionUID = 7146681148113043748L;
  private String mAuthor;
  private String mDescription;
  private Date mCreationDate;

  public Treet(String author, String description, Date creationDate) {
    mAuthor = author;
    mDescription = description;
    mCreationDate = creationDate;
  }

  @Override
  public String toString() {
    return String.format("Treet:  \"%s\" by %s on %s", 
                         mDescription, mAuthor, mCreationDate);
  }

  @Override
  public int compareTo(Object obj) {
    Treet other = (Treet) obj;
    if (equals(other)) {
      return 0;
    }
    int dateCmp = mCreationDate.compareTo(other.mCreationDate);
    if (dateCmp == 0) {
      return mDescription.compareTo(other.mDescription);
    }
    return dateCmp;
  }

  public String getAuthor() {
    return mAuthor;
  }

  public String getDescription() {
    return mDescription;
  }

  public Date getCreationDate() {
    return mCreationDate;
  }

  public List<String> getWords() {
    String[] words = mDescription.toLowerCase().split("[^\\w#@']+");
   return Arrays.asList(words); 

  }

  public List<String> getHashTags() {

  return getWordsPrefixedWith("#");
  }

   public List<String> getMentions() {
     return getWordsPrefixedWith("@");
   }

  private List<String> getWordsPrefixedWith(String prefix) {
  List<String> results = new ArrayList<String>();
    for (String word : getWords()) {
      if (word.startsWith(prefix)) {
       results.add(word); 
      }
  }
  return results;
         }


}  

Example.Java

import java.util.Arrays;
import java.util.Date;

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);
 Treet originalTreet = treets[0];
    System.out.println("Hashtags:");
    for (String hashtag : originalTreet.getHashTags()) {
    System.out.println(hashtag);
    }
  }

}
Dave Jindal
Dave Jindal
4,000 Points

Hey Denis,

i checked your code and also tested it in a new workspace. I couldn't find any errors and it also works fine on my workspace.

The code you posted created this output for me:

There are 58 treets.                                                                                           
Hashtags:                                                                                                      
#treet 

You might wanna check if you recompiled your code with the command javac Example.java and then execute it again with java Example in the command line interface.

Cheers Dave

1 Answer

Denis Madroane
Denis Madroane
2,983 Points

Thank you, Dave! The issue resolved itself after I logged out and in of Workspaces. I guess it was just a weird issue.