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 trialmelissakeith
3,766 PointsKeep getting error: unreachable statement in compareTo task
I've tried dateCmp and got the same error
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) {
BlogPost other = (BlogPost) obj;
return 1;
int compareCreationDate = mCreationDate.compareTo(other.mCreationDate);
if (compareCreationDate == 0) {
return mCreationDate.compareTo(other.mCreationDate);
}
return compareCreationDate;
}
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
melissakeith
3,766 PointsNevermind, this is what worked.
public int compareTo (Object obj) {
BlogPost other = (BlogPost) obj;
int compareCreationDate = mCreationDate.compareTo(other.mCreationDate);
if (equals(other)) {
return 0;
} else {
return mCreationDate.compareTo(other.mCreationDate);
}
}
Deividas Strioga
12,851 PointsHello Melissa, in the compareTo method you have 2 return statements. After the first one, return 1, the method breaks and the following code simply does not have a chance to run. I do not think that this return is necessary for your code to run, try removing this line. Cheers!
melissakeith
3,766 Pointspackage 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) {
BlogPost other = (BlogPost) obj;
int compareCreationDate = mCreationDate.compareTo(other.mCreationDate);
if (compareCreationDate == 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;
}
}
What I had to add was the compareTo method. The task is to "cast the object past to the compareTo method to a com.example.BlogPost. Check for quality first, if they are equal return 0. Leave the default return of one in place for this statement."
This is the error I keep getting.
./com/example/BlogPost.java:26: error: missing return statement
}
^
1 error
Deividas Strioga
12,851 PointsFirst part of the task - "cast the object past to the compareTo method to a com.example.BlogPost" - is done. And the second part has to be done the same way as in Craig's last video. "Check for quality first, if they are equal return 0". So you have to compare the passed in object to a casted one, new object, post in your case, in the if statement:
if (equals(post)) {
return 0;
}
If they are equal, the method will return 0 and will not continue. If they are not equal, the method continues. For now we just have to return 1 after the if statement.
melissakeith
3,766 PointsThank you, I tried that but I'm still getting an error.
public int compareTo (Object obj) {
BlogPost other = (BlogPost) obj;
return 1;
int compareCreationDate = mCreationDate.compareTo(other.mCreationDate);
if (equals(post)) {
return 0;
} else {
return mCreationDate.compareTo(other.mCreationDate);
}
}
and the error message is
./com/example/BlogPost.java:24: error: cannot find symbol
if (equals(post)) {
^
symbol: variable post
location: class BlogPost
1 error
I've tried every other option I could think of; Post, BlogPost, Object, obj, etc. nothing worked?
melissakeith
3,766 PointsI also tried removing the
return 1;
but still got the same error message
Deividas Strioga
12,851 PointsDeividas Strioga
12,851 PointsHey Melissa, It seems i made a mistake, instead of other i wrote post object. Sorry, it mislead you :) For task 2 this code should have been enough: