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

Juan Santiago
Juan Santiago
3,766 Points

I'm lost in the first question of the "Compare" Challenge.

The instructions asks me to " just make the class implement Comparable interface, and produce acompareTo method that accepts an object as a parameter." And I wrote this in the code:

public int comparteTo(Object obj){ BlogPost other = (BlogPost) obj; if(equals(other)){ return 1; } int dateComp = mCreationDate.compareTo(other.mCreationDate); return 1; }

But an error pops out telling me to be sure that the class implements the compare method. I don't get it T.T Help please!

com/example/BlogPost.java
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 int comparteTo(Object obj){
  BlogPost other = (BlogPost) obj;
    if(equals(other)){
    return 1;
    }
    int dateComp = mCreationDate.compareTo(other.mCreationDate);
    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

I see at least two errors:

  1. There is a typo. I will not say where yet because being able to find them is a good skill to have. :)
  2. Your class needs to have implements Comparable after public class BlogPost.
Juan Santiago
Juan Santiago
3,766 Points

Oh it works! now I feel so silly hahahaha. thnx a lot :D

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Juan;

Welcome to Treehouse!

I don't see where your BlogPost class has implemented the Comparable interface. The general syntax for that looks like: public class ClassName implements InterfaceName.

For Task 1, we only need to write the start of the method compareTo. There is also something you are missing in your syntax that was discussed at around the 6:54 in the video.

Post back if you are still stuck.

Happy coding,

Ken

Juan Santiago
Juan Santiago
3,766 Points

Thanks, I've successfully completed the challenge :D