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'm stuck

I don't know how to do it, I can't come up with the answer. Does anybody know how to do it? The question was: Create a new String variable named bestSport and set its value it to be the first element of the sports array.

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

3 Answers

Michael Salomon
Michael Salomon
1,356 Points

use [] instead of {};

String[] sports = { "Basketball", "Baseball", "Tennis" }; String bestSport = sports[0];

When you want to get an element from an array knowing the position of the element, you have to put the position in "[]".

For example:

String[] mobileOS = ["Android", "IOS"];
int position = 0;
String myOS = mobileOS[position];

"()" is commonly used to call a function.

Joe White
Joe White
9,750 Points

If I remember correctly, I had to do this with 3 lines of code for it to accept the result

what can I add to make the three lines?

Joe White
Joe White
9,750 Points

As per Yael' answer