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
Benjamin Young
5,881 PointsBad Source File
I am in Java Data Structures and have run into a bit of an issue.
When I try to import my com.teamtreehouse.Treets; I get the error:
javac Example.java && java Example
Example.java:5: error: cannot access Treets
import com.teamtreehouse.Treets;
^
bad source file: ./com/teamtreehouse/Treets.java
file does not contain class com.teamtreehouse.Treets
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
Onto the actual code Newly created Treets.java
package com.teamtreehouse.com;
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();
}
}
}
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 treet = new Treet(
"craigdennis",
"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.",
new Date(1421849732000L)
);
Treet secondTreet = new Treet(
"journeytocode",
"@treehouse makes learning Java sooooo fun! #treet",
new Date(1421849732000L)
);
System.out.printf("This is a new Treet: %s %n", treet);
System.out.println("The words are:");
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);
}
}
And lastly 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) {
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 String[] getWords() {
return mDescription.toLowerCase().split("[^\\w#@']+");
}
}
1 Answer
markmneimneh
14,133 PointsI am not in the data structure class but package com.teamtreehouse.com; is unusual ... do you have a speeling in there? It usually something like com.teamtreehouse?
Thanks
package com.teamtreehouse.com;
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(); } } }
Benjamin Young
5,881 PointsBenjamin Young
5,881 PointsYep that is exactly right Mark, I noticed that after I looked at my code in a markup text. Thank you for pointing that out as well, if I hadn't noticed that you would have still been a big help!