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

I don't know how to do that

I don't know to do this question

Array.java
String[] sports = { "Basketball", "Baseball", "Tennis" };
String bestSport = "";
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(sports.length);
bestSport = sports[0];

3 Answers

Dean Silvestro
PLUS
Dean Silvestro
Courses Plus Student 20,733 Points

You're overthinking it. You actually already finished the first part of the challenge if you get rid of the randomGenerator and randomNumber lines. You don't need randomness when you have a specific target, in this case the first element of the sports array.

If you're still stuck I will leave the solution below:

String[] sports = { "Basketball", "Baseball", "Tennis" };
String bestSport = sports[0];
int numberOfSports = sports.length;
String lastSport = sports[2];

Hi there,

You start with an array of Strings. The question is Create a new String variable named bestSport and set its value it to be the first element of the sports array.

That's:

String bestSport = sports[0];

Next up, 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.

int numberOfSports = sports.length;

Lastly, Declare a String variable named lastSport and set it to be the last element of the sports array. Now, you could just access sports[2] because you know how many elements are in the array. Alternatively, you can use the numberOfSports variable like this:

String lastSport = sports[numberOfSports - 1];

Let me kow how you get on.

Steve.

Thanks, I understand now.