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

System.out.println(hashtag) isn't printing 'hashtag' - I can't find the error in my code.

I've come to printing out the hashtags - Although there isn't an error flagging up in my code, I'm not getting any returned results like Craig is. All I'm getting is

There are 58 treets Hashtags:

Would someone be able to have a look over my code & find where I've gone wrong. I can't seem to spot anything?

Thank you

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);
    }
  }
}

Treets.java

package com.teamtreehouse;

import java.io.*;

public class Treets {
  public static void save(Treet[] treets) {
    try (
      FileOutputStream fos = new FileOutputStream("treets.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fos);
    ) {
      oos.writeObject(treets);
    } catch(IOException ioe) {
      System.out.println("Problem saving Treets");
      ioe.printStackTrace();
    }
  }

  public static Treet[] load() {
    Treet[] treets = new Treet[0];
    try (
      FileInputStream fis = new FileInputStream("treets.ser");
      ObjectInputStream ois = new ObjectInputStream(fis);
    ) {
      treets = (Treet[]) ois.readObject();
    } catch(IOException ioe) {
      System.out.println("Error reading file");
      ioe.printStackTrace();
    } catch(ClassNotFoundException cnfe) {
      System.out.println("Error loading treets");
      cnfe.printStackTrace();
    }
    return treets;
  }

}

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<Treet>, 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(Treet other) {;
    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;
  }



}

1 Answer

Emmanuel C
Emmanuel C
10,636 Points

Hi,

It looks like in your getWordsPrefixedWith(string prefix) method when you check if (word.startsWith("prefix")), youre checking if the word starts with the literal string prefix instead of the passed in argument prefix variable, which what i assume you want to check. Try using the variable instead and see if that fixes it.

Assuming that none of the words in your split mDescription actually start with "prefix" like "prefix_example", then no word would ever be added to results and your for loop in your main method will iterate 0 times and never print anything. Which is what I believe the problem youre experiencing.

Hope this helps.

You are not going to believe how long I've scoured my code looking for that error.

You're 100% correct. I feel such an idiot now though.

Thank you for taking the time to look through my code!