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

Can Someone Help?

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

Array.java
String[] sports = { "Basketball", "Baseball", "Tennis",};
String bestSports = sports[0];
int numberOfSports = sports.length;
String lastSports = sports[2];

2 Answers

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Rohan

It looks as if you've simply put an extra comma inside your array, you should clear any compile errors after removing the last comma, on a separate note obtaining the last index of the array dynamically can be done by using the length -1 (demonstrated in the code below) which will help in real world situations.

String[] sports = { "Basketball", "Baseball", "Tennis"};
String bestSports = sports[0];
int numberOfSports = sports.length;
String lastSports = sports[(numberOfSports - 1)];

Hope this helps

Daniel

No It Didnt Help :(

Derek Crosson
Derek Crosson
991 Points

I'm not a Java programmer so this might not be correct but it's worth a try.

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

Derek Crosson
Derek Crosson
991 Points

Sorry, I didn't read the challenge... Your variable name should be bestSport not bestSports

Thanq Derek It Helped :)