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 Exploring the Java Collection Framework ArrayLists

Carlo Antonio Bilbao
Carlo Antonio Bilbao
24,113 Points

Java, ArrayList Challenge

Not sure how to fix the compile error. Code looks good to me.

com/example/BlogPost.java
package com.example;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Date;

public class BlogPost implements Comparable<BlogPost>, Serializable {
  private String mAuthor;
  private String mTitle;
  private String mBody;
  private String mCategory;
  private Date mCreationDate;

  private List<String> getExternalLinks("http") { 
    List<String> results = new ArrayList<String>();
    for (String word : getWords()) {
      if (word.startsWith("http")) {
        results.add(word);
      }
    }
    return results;
  }

  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 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;
  }
}
Craig Dennis
Craig Dennis
Treehouse Teacher

What's the compilation error?

4 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

private List<String> getExternalLinks("http") {

There is something wrong with this line....looks like you are passing a value into the method declaration.

That help?

Marcus Karlsson
Marcus Karlsson
1,291 Points

If you fix that error it complains about missing to implement the getExternalLinks method? Why?

Marcus Karlsson
Marcus Karlsson
1,291 Points

You have to define what kind of datatype getExternalLinks takes in as a paramameter. While writing "http" it will of course know it's a string but writing it again in word.startsWith("http") it won't be able to know that u are refering to the "http" in the parameter field.

Since u will call the method getExternalLinks when u want to check a string in your text u have to write getExternalLinks(String text) since u wanna receive a string. Then later in your word.startWith u need to refer to the parameter u are taking in which is word.startWith(text).

Also as craig said u are passing a value of a string not the datatype String that u wanna pass in ur method

Here's the answer that allowed me to pass.

  public List<String> getExternalLinks () {
    List<String> links = new ArrayList<String>();
    for (String link : getWords()) {
      if (link.startsWith("http")) {
        links.add(link); 
      }
     }
    return links;
  }

Part of the problem is that the method has been set to private. If it's private then it can only be accessed from within the class. The other problem is that there's a parameter being passed into it and not being used. We already know that the method should retrieve everything starting with "http" so we can use that within the method at 'link.startsWith("http")' and not have to pass a param at all.