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

HELP... why won't my code work?

The question is: Alright, now let's order the blog post's creation date chronologically, oldest first.

NOTE: The app where this class is being used ensures that no two blog posts are posted at the same time, so you do not need to worry about any edge cases.

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(BlogPost other) {
    if (equals(other)) {
      return 0;
    }
    return mCreationDate.compareTo(other.mCreationDate);
  }

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

3 Answers

Hmm , I think Craig Dennis Explain Better Than me :'(

https://teamtreehouse.com/community/how-do-i-do-this-15

You can try my code , It pass the challenge

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

 @Override
 public int compareTo(Object obj){
    BlogPost other = (BlogPost) obj;
    if (this.equals(other)){
      return 0;
    }
    return mCreationDate.compareTo(other.mCreationDate);
  }

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

I'v tried using both @Override and not using it, either way it still wont work.

Any other suggestions?

Hi Josh ,

Can you change the argument to accept generic Object instead of BlogPost and later Cast Them To BlogPost.

To be honest ,

I also confuse why we have to use Generic Object and Later Cast Them To BlogPost.

Why we can't simply use BlogPost and compare them.

Craig Dennis , Can you explain us please

T^T

I'v changed the parameter to accept an Object but am unsure how the syntax would look to cast the object as an BlogPost

Oh , You can cast Object To BlogPost using this syntax ;)

   BlogPost other = (BlogPost) obj;

Did your code pass writing it out that way? I'v looked at other peoples code for this challenge and you all seem to write the same thing. Do you think it could be some sort of bug?

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Hey Josh,

What step of the challenge are you getting stuck on? Looks like there are three steps to the challenge. I can help you if you let me know which one you're stuck on.

Thanks!

Thanks i'm stuck on the 3rd step

Hi Josh Parker , I rewatch the video many times. From What I guess is when we use :

If we only create compareTo For BlogPost Object.

 public int compareTo(BlogPost obj){

We will get this error because mCreationDate is a Date Object.

BlogPost is not abstract and does not override abstract method compareTo(Object) in Comparable

If we look into the Comparable Interface, The CompareTo Method , It takes Any Object

int compareTo(T o)

References : http://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html#compareTo-T-

and If we look into the CompareTo Method in Date Object , It has function that sort and compare date. References : http://docs.oracle.com/javase/8/docs/api/java/util/Date.html#compareTo-java.util.Date-

So If we create a compareTo Method That Take Object instead of BlogPost. We can Compare 2 Date Object.

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

References : http://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html http://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

It might seem a bit confusing but I do some research and I think it start to shine some light now

Mustafa Alordowny
Mustafa Alordowny
4,495 Points
public int compareTo(Object obj) {
 BlogPost other = (BlogPost) obj; 
if (equals(other)) { 
return 0; 
} 
return mCreationDate.compareTo(other.mCreationDate); 
\\ I have passed the challenge using this code, if you dont understand it just reply to my answer

I'm trying to mirror the way Dennis does it in the video at the moment and it looks like this:

public int compareTo(Object obj) { if (equals(obj)) { return 0; } BlogPost other = (BlogPost) obj; int dateCmp = mCreationDate.compareTo(obj.mCreationDate); if (dateCmp == 0) { return mDescription.compareTo(obj.mDescription); } return dateCmp; }

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Try casting to a BlogPost object immediately before testing for equality. I believe the code challenge only asks for you to order the BlogPost objects by creation date, so you don't need to compare mDescription - just mCreationDate.

Let me know if that helps!