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

exploring the java collection framework/arraylists

Challenge Task 1 of 1

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!

http://teamtreehouse.com/library/java-data-structures/exploring-the-java-collection-framework/arraylists

Ken Alger
Ken Alger
Treehouse Teacher

Isaiah;

Can you post the code you have tried?

Ken

I just don't get what to do.

6 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Isaiah;

Task 1

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!

The task is asking us to create a new method that will go through a list of words and determine if they are links (designated in this case by the string of characters starting with http). Our steps, or plan of attack, would be something along the lines of:

  1. Create a method that returns a list of strings.
  2. Define a variable in which to store our results.
  3. Write a for loop to loop through our words... perhaps this would be a good place for the getWords() method.
  4. Inside our for loop we need to check if our word (links) starts with http. If you recall there is a handy method in Java called startsWith(), perhaps that would be helpful.
  5. Return our results.

Hopefully that will get you going in the correct direction. Post back if you are still stuck.

Happy coding, Ken

Hello,

I am also stuck on this task. I think I have writen the code correctly, I have pretty much followed your guidelines and used the work from the lesson before this task as a reference, but no matter what I seem to do at this stage all that is returned to me is this error message:

./com/example/BlogPost.java:69: error: class, interface, or enum expected } ^ Note: JavaTester.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error

I figured this was telling me that I have missed a '}' at some point in my code, but I have scanned it over and over, and I can't find anything to be missing. I have also added and taken away some random '}' at certain points as a test, and when I do that I'm returned a massive list of errors. So I've managed to bring it down to this one error that I'm unable to resolve.

Can someone help me please?

Frazer.

Hi Frazer Watson, could you please post your code?

Yigit Cakar
PLUS
Yigit Cakar
Courses Plus Student 5,975 Points

Hello, my passing solution is as follows, I hope it helps.

Add necessary imports: import java.util.List; import java.util.ArrayList;

   public List<String> getExternalLinks(){
    List<String> links = new ArrayList<String>();
    for (String words:getWords()){
      if(words.startsWith("http")){
        links.add(words);
      }
    }
    return links;
  }
Gonzalo Torres del Fierro
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

my logic was exactly like yours...mmm it is that normal? i mean your solution was exactly the same as mine, except the variable names...well nice to know. regards.

I don't know how to post the pictures from the actual code like every other post seems to do. I can copy and paste it for you if you want?

Just copy and paste it, then add

```.java in front of it, and ``` behind it. (Make sure you press return(on Mac) 
or enter(on Windows) after ```.java or before ```.)

that will open a block

like this.

code: (note the spaces)

that will open a block [space]

front of the block: ```.java [space]

like this. [space]

end of the block: ```

I can't copy and paste it, my computer won't give me the option when I right-click, and Ctrl + c/Ctrl + v just do nothing on the code in my treehouse task screen.

Christopher Sullivan
Christopher Sullivan
4,230 Points

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

I dont get why but this worked for me as well. My original code looked exactly like this then swapped this in and BOOM. It worked.