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 trialRenan Coutinho
1,373 PointsCreate a new String variable named bestSport and set its value it to be the first element of the sports array.
I've tried all options and did not work
String[] sports = { "Basketball", "Baseball", "Tennis" };
String[] bestSport = { "Ab", "Abc", "Abcd"};
2 Answers
Chase Marchione
155,055 PointsHi Renan,
1) The bracket ('[]') notation indicates an array. Since bestSport needs to be a String variable rather than a String array, we don't need those brackets when declaring the bestSport variable.
2) To set the value of the bestSport String variable to be the first element of the sports array, there are a couple things to consider:
- Arrays 'start at' 0, meaning that the first element (item) in the array is considered to be at the 0th place.
- We want to tell the compiler to set the value of the bestSport string to whatever the value of that 0th item in the sports array is. To do this, you need the bracket notation, with the number of the element written inside those brackets.
String[] sports = { "Basketball", "Baseball", "Tennis" };
String bestSport = sports[0];
Hope this helps!
Renan Coutinho
1,373 PointsThanks a lot. I understand wht i said but i am having others problems now haha.
Declare (AKA create) an int variable named numberOfSports and set it to the number of elements in the array using the array's length property.
I am using it, can u explain me again where i am missing?
String[] sports = { "Basketball", "Baseball", "Tennis" };
String bestSport = sports[0];
int numberOfSports = randomGenerator.nextInt(sports.length);
pankaj kumar
Courses Plus Student 367 PointsRemove the last line from the code...