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 PointsCan someone explain what is wrong with my code or what's missing in java data structures order dates task.
These are the errors I keep getting, not sure what is expected.
./com/example/BlogPost.java:32: error: expected
Arrays.sort(blogPosts);
^
./com/example/BlogPost.java:32: error: expected
Arrays.sort(blogPosts);
^
./com/example/BlogPost.java:32: error: cannot find symbol
Arrays.sort(blogPosts);
^
symbol: class blogPosts
location: class BlogPost
./com/example/BlogPost.java:32: error: cannot find symbol
Arrays.sort(blogPosts);
^
symbol: class sort
location: class Arrays
./com/example/BlogPost.java:31: error: cannot find symbol
BlogPost[] blogPosts = {blogPost, secondBlogPost};
^
symbol: variable blogPost
location: class BlogPost
./com/example/BlogPost.java:31: error: cannot find symbol
BlogPost[] blogPosts = {blogPost, secondBlogPost};
^
symbol: variable secondBlogPost
location: class BlogPost
6 errors
package com.example;
import java.util.Date;
import java.util.Arrays;
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;
int compareCreationDate = mCreationDate.compareTo(other.mCreationDate);
if (equals(other)) {
return 0;
} else {
return mCreationDate.compareTo(other.mCreationDate);
}
}
BlogPost[] blogPosts = {blogPost, secondBlogPost};
Arrays.sort(blogPosts);
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
Julian Garcia
18,380 PointsHi,
you just need to include the comparison of dates inside if statement:
@Override
public int compareTo(Object obj) {
BlogPost blogpost =(BlogPost) obj;
if(equals(blogpost)) {
return 0;
}
int res = mCreationDate.compareTo(blogpost.mCreationDate);
if(res==0){
return mBody.compareTo(blogpost.mBody);
}
return res;
}
}
Hope this help.
Patrick VanVorce
1,952 PointsHey Melissa,
Make sure at the end of the method compareTo, return res or whatever variable name you used. If you have it say return 1 at the end of the method, you will continue to see that message saying "Expected -1 but received 1, ... "
James Hughes
3,204 Pointsthis is driving me insane!!!!!!!! my code is the same at this but yet im gettin g20 errors this was not very well explained in the tutorials at all. They expect you to know, if I knew i wouldn't be watching these tutorials.
melissakeith
3,766 Pointsmelissakeith
3,766 PointsI tried this code but still got an error message 'Expected -1 but received 1, when comparing Sun Jul 20 20:18:00 UTC 1969 to Mon Mar 09 16:00:00 UTC 2015'
melissakeith
3,766 Pointsmelissakeith
3,766 Pointsok that worked thanks, can you explain what return res is doing?
Patrick VanVorce
1,952 PointsPatrick VanVorce
1,952 PointsMelissa,
It is returning the result from the compareTo of the Creation Dates.
Julian Garcia
18,380 PointsJulian Garcia
18,380 PointsThe logic will be something like: 1.- Compare if the current object is equal to blogpost. If they are equal each other then return 0.
2.- if they are not the same, compare creation date, mCreationDate, of both.
int res = mCreationDate.compareTo(blogpost.mCreationDate);
3.- if both creation dates are equal then test the equality of the body
if none of the above passed then return the first comparison that failed, in this case the result in res:
return res;