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 trialISAIAH S
1,409 Pointsexploring 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!
ISAIAH S
1,409 PointsI just don't get what to do.
6 Answers
Ken Alger
Treehouse TeacherIsaiah;
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 itgetExternalLinks
.NOTE: Remember to import the needed classes from
java.util
. HINT: We already made thegetWords
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:
- Create a method that returns a list of strings.
- Define a variable in which to store our results.
- Write a for loop to loop through our words... perhaps this would be a good place for the
getWords()
method. - 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. - Return our results.
Hopefully that will get you going in the correct direction. Post back if you are still stuck.
Happy coding, Ken
Frazer Watson
1,480 PointsHello,
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.
ISAIAH S
1,409 PointsHi Frazer Watson, could you please post your code?
Yigit Cakar
Courses Plus Student 5,975 PointsHello, 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
Courses Plus Student 16,751 Pointsmy 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.
Frazer Watson
1,480 PointsI 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?
ISAIAH S
1,409 PointsJust 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: ```
Frazer Watson
1,480 PointsI 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
4,230 Pointspublic 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.
Ken Alger
Treehouse TeacherKen Alger
Treehouse TeacherIsaiah;
Can you post the code you have tried?
Ken