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 trialGoran Penov
1,635 PointsArray of strings problem.
Hi, I'm watching Introduction to arrays and I can figure out what am I doing wrong in the challenge task. It says: 'Declare an array of string named sports and name three sports". My code is: String[] sports = { "Soccer, basketball, darts." };
Can someone help me? Tnx.
3 Answers
Gunjeet Hattar
14,483 PointsHi Goran,
First to correct your error
String[] sports = { "Soccer, basketball, darts." }; //this is incorrect explanation below
//Correct Way
String[] sports = { "Soccer", "basketball", "darts" };
First understand what is a String literal. Anything within double quotes is a string literal "anything_inbetween"
Now when you say
String[] sports = { "Soccer, basketball, darts." };
Java will consider "Soccer, basketball, darts." as one element of an array, rather than three. If you separate them out like
{"Soccer", "basketball", "darts" }; this means that these are three String elements
Hope this helps
Chris Shaw
26,676 PointsHi Goran,
You're very close, the only problem is you have a single string value in you're array instead of three separate strings.
String[] sports = { "Soccer", "basketball", "darts." };
EDIT: Gunjeet beat me to it.
Goran Penov
1,635 PointsThanks Gunjeet and Chris, my bad, lack of concentration :)