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 trialSameer Rao
490 PointsBummer! Expected -1 but received 1, when comparing Sun Jul 20 20:18:00 UTC 1969 to Mon Mar 09 16:00:00 UTC 201
Mentioned below is my piece of code which is probably gving this error. Any insights please? @Override public String toString() { return String.format ("Treet: \"%s\" by %s on %s", mTitle,mCategory, mCreationDate);
}
package com.example;
import java.util.Arrays;
import java.util.Date;
public class BlogPost implements Comparable {
private String mAuthor;
private String mTitle;
private String mBody;
private String mCategory;
private Date mCreationDate;
public BlogPost(String author, String title, String body, String category, Date creationDate) {
mAuthor = author;
mTitle = title;
mBody = body;
mCategory = category;
mCreationDate = creationDate;
}
@Override
public String toString() {
return String.format ("Treet: \"%s\" by %s on %s", mTitle,mCategory, mCreationDate);
}
@Override
public int compareTo(Object obj) {
BlogPost other = (BlogPost ) obj;
if (equals(other)){
return 0 ;
}
return 1;
}
public String[] getWords() {
return mBody.split("\\s+");
}
public String getAuthor() {
return mAuthor;
}
public String getTitle() {
return mTitle;
}
public String getBody() {
return mBody;
}
public String getCategory() {
return mCategory;
}
public Date getCreationDate() {
return mCreationDate;
}
}
2 Answers
Philip Gales
15,193 PointsThis forum post has a lively discussion talking about this answer.
Mauro Teixeira
3,727 PointsWell, notice that your compare function does the following: If this object equals the object you are passing to the method, then it returns 0; Otherwise, if it doesn't match, it returns 1.
The exercise asks you to write a function that orders blogposts by their creation dates. So what you want to do is compare the creation dates, and remember that usually the compareTo method returns -1, 0 or 1 to establish an order between the object and the one you are comparing to.
Also, a Date object by itself also implements the Comparable interface, meaning it also has a compareTo method, so here's a possible solution:
@Override
public int compareTo(Object obj) {
BlogPost other = (BlogPost) obj;
if (equals(other)){
return 0 ;
}
return mCreationDate.compareTo(other.mCreationDate);
}
In the code you posted, you always returned 1 if the blogposts were different, so there was no way to order the blogposts, whether you did x.compareTo(y) or y.compareTo(x), the result would always be one.
Now, with the code I posted, what will happen is that x.compareTo(y) will compare the creationDates of x and y, it will return -1 if x's creationDate is before y's creation date, it will return 0 if the blog posts are equal or if the dates are the same, and it will return 1 if x's creation date is after y's creation date.