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 Using ArrayLists

returns compile error... why bolean instead of string?

this course started to be confusing very fast....

./com/example/BlogPost.java:39: error: incompatible types: boolean cannot be converted to List return results.add(word); ^ Note: JavaTester.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error

Here is a code that gives me problems:

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

3 Answers

Parth Sehgal
Parth Sehgal
362 Points

Your method is:

public List<String> getExternalLinks()

You specify the return type as

List<String>

In your function, you are trying to add every word that starts with 'http' into your array called results.

The problem is here:

return results.add(word);

That should just be:

results.add(word);

The add function returns true if the collection is changed by the add operation. But you specified that your function returns

List<String>

earlier. So that's the problem. You should just add to the results array, you can check the return value if you want, but it probably isn't necessary.

J.D. Sandifer
J.D. Sandifer
18,813 Points

Parth, returning the List<String> is part of the requirements of the problem so that should stay the same. Your correction to the premature return statement is all Mijurg needs to change.

Parth Sehgal
Parth Sehgal
362 Points

Right, I was just saying that he could check the return value of results.add(word) and make sure that it is true if he really wanted to. But it's probably not necessary.

Thank You all, i already figured that out. But reading documentation is so confusing sometimes... This is the first time i got into a problem i couldnt solve on my own for a long long time. Thank you again.