Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App (2014) Coding the Fun Facts Using an Array

Renan Coutinho
Renan Coutinho
1,373 Points

Create 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

Array.java
String[] sports = { "Basketball", "Baseball", "Tennis" };

String[] bestSport = { "Ab", "Abc", "Abcd"};

2 Answers

cm21
cm21
150,854 Points

Hi 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
Renan Coutinho
1,373 Points

Thanks 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);

Remove the last line from the code...