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 trialGaspar Santiago
3,377 Points??? When an object `equals` another, the compareTo method should return 0. Current method returned 1. ??? Current error
This exercise is very confusing and I'm getting now answers and can't move on.
Here's my source code:
package com.example;
import java.util.Date;
public class BlogPost implements Comparable //just make the class implement Comparable interface, { 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; }
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; }
//Let's allow our BlogPost to be sorted. In this first step, //and produce a compareTo method that accepts an object as a parameter. public int compareTo(Object obj) { //In this step, cast the Object passed to the compareTo method to a com.example.BlogPost. //Check for equality first, if they are equal return 0. BlogPost post = null;
if(post == obj){
post = (BlogPost) obj;
}
return 1;//Leave the default return of 1 in place for this step.
} //Have it always return 1 for this first step, we'll do more in the next step. }
As you can see by the comments in the code, I'm following the instructions to the T and getting no where. I can't have two separate returns yet neither a return of 1 or 0 will work for this exercise.
package com.example;
import java.util.Date;
public class BlogPost {
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;
}
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
Roland Kalmogo
25,657 PointsHello Gaspar,
You should cast your object first before trying to check whether they are equal. The way you did it is wrong because without casting first, you will never get the compared object be equal to the comparing one even if they are equal.
@Override public int compareTo(Object obj){ BlogPost post = (BlogPost) obj;// casting first if(equals(obj)){//comparing equality return 0; } return 1; //they way they want you to do }
Roland Kalmogo
25,657 PointsYour comparison is wrong:
It is not post.equals(obj) but ## this.equals(post) or ## equals(post)
You can use equals method directly because you're already in BlogPost class. Right?
It should work !!
Gaspar Santiago
3,377 PointsGaspar Santiago
3,377 PointsHi Roland... did what you said and still nothing. Current error: Oops! It looks like Task 1 is no longer passing. Source code below.
@Override public int compareTo(Object obj)//produce a compareTo method that accepts an object as a parameter. { BlogPost post = (BlogPost) obj;//In this step, cast the Object passed to the compareTo method to a com.example.BlogPost.
}