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
Joseph Wasden
20,407 PointsUsing ArrayLists - "Expected 3 links to be found, but received 13" error?
(Side note: I can't seem to get the Get Help button to work when asking questions. when I click it, it changes to dark green, then nothing happens.)
I am working on the Java ArrayLists challenge, and I've whittled out all of my error messages. However, when I check my work I get an error of ""Bummer! Expected 3 links to be found, but received 13."
I'm not really sure how that is a problem. My first thought is, "well, if I found 13, there are 13 to be found!" but Since this is a pretty controlled challenge, I doubt that's the right approach. :)
Perhaps I need to refine my startsWith() paramenter of "http" ? but, that is the param suggested in the notes.
Any idea as to why I would get this error? I've included the code, and commented at areas I've added.
package com.example;
import java.io.Serializable;
import java.util.Date;
import java.util.ArrayList; //added for the challenge
import java.util.List; // added for the challenge
public class BlogPost implements Comparable<BlogPost>, Serializable {
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(BlogPost other) {
if (equals(other)) {
return 0;
}
return mCreationDate.compareTo(other.mCreationDate);
}
/*
Let's add a method that returns a list of all links in the body of the document.
All words that start with http should be considered links. Name it getExternalLinks.
NOTE: Remember to import the needed classes from java.util.
HINT: We already made the getWords method, we should use it!
*/
//startcode
public List<String> getExternalLinks() {
List<String> linkResults = new ArrayList<String>();
for (String word : getWords()) {
if (word.startsWith("http")) {
}
linkResults.add(word);
}
return linkResults;
}
//endcode
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
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsLet me give you a hint: You have error in this method. Do you see how your if() has empty body: no code inside braces. That's why instead of using if you are adding ALL words but not the ones that start with "http". Do you see where I lead to and how to fix this?
//startcode
public List<String> getExternalLinks() {
List<String> linkResults = new ArrayList<String>();
for (String word : getWords()) {
if (word.startsWith("http")) {
}
linkResults.add(word);
}
return linkResults;
}
Joshua marais
2,955 Pointstry this it passes task 1 & 2. paste the code at the bottom
public int compareTo(Object obj){ BlogPost other = (BlogPost) obj; if (!equals(other)){ return 1; }else{ if(equals(other)) return 0; } return mCreationDate.compareTo(other.mCreationDate); } }
Anand Altekar
1,790 PointsWhat am I doing wrong here? I get the same exact error mentioned above.
package com.example;
import java.io.Serializable; import java.util.Date; import java.util.List; import java.util.Arrays; import java.util.ArrayList;
public class BlogPost implements Comparable<BlogPost>, Serializable { 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(BlogPost other) { if (equals(other)) { return 0; } return mCreationDate.compareTo(other.mCreationDate); }
public List<String> getExternalLinks() { List <String> results = new ArrayList <String>(); for (String word : getWords()) { if (word.startsWith("http")); { results.add(word); } } return results; }
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; } }
Joseph Wasden
20,407 PointsJoseph Wasden
20,407 PointsOh man. I don't know how I missed that. Thanks. :)