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 trial

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