Java Java Data Structures Organizing Data Comparable

Adam Shockley
Adam Shockley
3,896 Points

Bummer: The `compareTo` method should currently always return 1

Getting this error on the step that specifically asks that we return zero if casted obj is equal to BlogPost. I'm clearly missing something here.

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

  @Override
  public int compareTo(Object obj) {

    if(obj == (BlogPost)obj){
        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;
  }
}

1 Answer

Adam Shockley
Adam Shockley
3,896 Points

My cast was just all wrong. Error was unhelpful on this one though.