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 me on this one i am mixing things up

Let's allow our BlogPost to be sorted. In this first step, just make the class implement Comparable interface, and produce a compareTo method that accepts an object as a parameter. Have it always return 1 for this first step, we'll do more in the next step.?

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;
  }
@Override
  public String[] getWords() {
    return mBody.split("\\s+");
  }

  public String getAuthor() {
    return mAuthor;
  }

  public String getTitle() {
    return mTitle;
  }

  public String getBody() {
    return mBody;
  }

  @override
  public int campareTo(Object obj){
    BlogPost other = (BlogPost) obj;
    return 1;
  }

  public String getCategory() {
    return mCategory;
  }

  public Date getCreationDate() {
    return mCreationDate;
  }
}

You write "campareTo" instead of "compareTo"

5 Answers

hello i have just done the first part of the code challenge but in my code am not using @Override in my code but i still pass the code challenge why is it letting me pass without @Override ? here is my code

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

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

its because your code is correct but later in the next stages it might tell you that the test 1 is no longer passing

Allan Clark
Allan Clark
10,810 Points

Check out this post i made on another forum question. Let me know if there is anything else I can clear up.

https://teamtreehouse.com/forum/understanding-the-concept

/com/example/BlogPost.java:24: error: method does not override or implement a method from a supertype @Override

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Try changing @override to @Override. Case matters.

Also, @Override doesn't belong on getWords, just on compareTo.

Hope it helps!

/com/example/BlogPost.java:24: error: method does not override or implement a method from a supertype @Override its giving me this error Found out that i heard forgoten to implement the comparable from the class BlogPost . Thanx a lot craig

Schwartz Prince
Schwartz Prince
Courses Plus Student 2,132 Points

Hi, Is that necessary to write @Override in the code? What will happen if we not writing the same in the code? Please explain

I copied and pasted the answer above, and the error message said that the class needs to implement the Comparable method; what exactly is it asking me to do?

Craig Dennis
Craig Dennis
Treehouse Teacher
// Assuming the 'implements Comparable' portion
public class BlogPost implements Comparable {
//...
}
Casey Huckel
PLUS
Casey Huckel
Courses Plus Student 4,257 Points

I get 5 errors from this:

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

@Override public compareTo(Object object) { BlogPost other = (BlogPost) obj; return 1; } }