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 Organizing Data Splitting Strings

luis martinez
luis martinez
2,480 Points

tweet.getWords() does not work

System.out.println("The words are: " tweet.getWords());
    for (String word: tweet.getWords())

How come when i do tweet.getWords()); it does not work but when i do it on the for command it works. I am confused.

[MOD: added ```java formatting -cf]

2 Answers

Brendon Butler
Brendon Butler
4,254 Points

Remove 'tweet.getWords()' located after "The words are: ".

This should cause a compiling error due to the incorrect Java syntax.

The reason is that System.out.println expects a String. To provide a String in your example you'd have to concatenate the String "The words are:" with the output of tweet.getWords() by using the + sign.

System.out.println("The words are: " + tweet.getWords());

Keep in mind that tweet.getWords() returns a String array. Arrays default toString() method usually don't return a nice String for their elements. Here you could use your System.out.println method for example to print every String within your for loop (taking one String out of the array once).

Hope this helps,