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 Comparable

Neil Gordon
Neil Gordon
8,823 Points

Compare to date question ??

public int compareTo(Object obj) { if(equals(obj)) { int compareDate = mCreationDate.compareTo(Object.mCreationDate); return compareDate; return 0;

}
return 1; 

}

com/example/BlogPost.java
package com.example;

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

  public int compareTo(Object obj) {
    if(equals(obj)) {
     int compareDate = mCreationDate.compareTo(Object.mCreationDate);
      return compareDate; 
      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;
  }
}

4 Answers

Allan Clark
Allan Clark
10,810 Points

First you will need to cast the parameter into a BlogPost Object. With the variable stored as an Object, it does not have access to the methods and variables you will need to do the comparison.

BlogPost bp = (BlogPost) obj;

for step 2 you only need to return 0 if they are equal so it would look something like this:

if(equals(bp)) {
      return 0;
    }
Neil Gordon
Neil Gordon
8,823 Points

Thank you , I updated the typecasting here is my new code

public int compareTo(Object obj){
BlogPost bp = (BlogPost) obj; 

  if (equals(bp)) { 
    return 0; 

  }
    return 1; 

  int compareDate = mCreationDate.compareTo(bp.mCreationDate);
      return compareDate;
}```

since i type casted bp inserted it into the *compareDate* method if this is wrong then I will have to take some more practice. *replacing Object with bp* 

below is my new error 
/com/example/BlogPost.java:28: error: unreachable statement
  int compareDate = mCreationDate.compareTo(bp.mCreationDate);
      ^
1 error

i am not sure why I cannot reach this statement. I see the arrow pointing to compareDate.
Neil Gordon
Neil Gordon
8,823 Points

Expected -1 but received 1, when comparing Sun Jul 20 20:18:00 UTC 1969 to Mon Mar 09 16:00:00 UTC 2015

Allan Clark
Allan Clark
10,810 Points

Java returns and exits the method whenever it hits the first return keyword.

  if (equals(bp)) { 
    return 0; 

  }
    return 1; 

  int compareDate = mCreationDate.compareTo(bp.mCreationDate);
      return compareDate;

So written like this, the method returns 0 if they are equal, if they are not equal it skips the if statement and hits the return 1; line. So it is impossible for the JVM control to reach what you have below it. You can also just directly return the comparison, to skip having to create a variable and return the variable. Try replacing the return 1 line with this:

// remember mCreationDate is a private variable so for bp you have to use the accessor method 
return mCreationDate.compareTo(bp.getCreationDate());
Neil Gordon
Neil Gordon
8,823 Points

@craigsdennis can you help when you have a moment not quite sure how to push forward with the above errors