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 Organizing Data Serialization

Dorka Tamas
Dorka Tamas
5,085 Points

Several errors

Hi,

I got several errors in this exercise, I tried to rewatch the video and correct the mistakes, but I can't really manage it.

Can you help me please?

Thanks a lot!

This is my code: https://teamtreehouse.com/workspaces/16377412

Your workspaces link is broken.

2 Answers

Dorka Tamas
Dorka Tamas
5,085 Points

It works for me, I am adding my code. Thanks.

Example.java: import java.util.Date; import java.util.Arrays;

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

public class Example {

public static void main(String[] args) { // Treet treet = new Treet( // "craigsdennis", // "want to be famous? Simply tweet about Java and use " + // "the hashtag #treet. I'll use tweet in a new " + // "@treehouse course about date structures.", // new Date(1421849732000L)
// ); // Treet secondTreet = new Treet( // "journeytocode", // "@treehouse makes learning Java sooooo fun! #treet", // new Date(1421878767000L) // ); // System.out.printf("This is a new Treet: %s %n", treet); // System.out.println("These are the words:"); //
// for (String word: treet.getWords()) { // System.out.println(word); // } // Treet[] treets = {treet, secondTreet}; // Arrays.sort(treets); // for (Treet exampleTreet: treets) { // System.out.println(exampleTreet); // } // Treets.save(treets); Treet[] reloadedTreets = Treets.load(); for (Treet reloaded : reloadedTreets) { System.out.println(reloaded); } }

}

Treet.java

package com.teamtreehouse;

import java.io.Serializable; import java.util.Date;

public class Treet implements Comparable, Serializable { private String mAuthor; private String mDescription; private Date mCreationDate;

public Treet(String author, String description, Date creationDate) { this.mAuthor = author; this.mDescription = description; this.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 String[] getWords() { return mDescription.toLowerCase().split("[^\w#@']+"); }

}

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

}

}

What errors are you getting specifically? paste your console output. An easy way to fix your code would be to just download the project files for your specific video. and then copy and paste into your ide/text editor. When I was completing the java courses that was a faster solution when I couldn't find the error myself.

Dorka Tamas
Dorka Tamas
5,085 Points

Sorry I cant paste is, but basically these are the error messages:

In the treets.java file - it says 17: illegal start of expression, + ";" expected, 18: variable treets is already defined in method save(Treet[]) 31: unexpected return value - return treets; in Example.java 34: Error cannot find symbol Treets.load