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

Trouble with declaring a string

Hello I am struck on one of the challenges and can not figure it out for the life of me. The question is.....

Declare a String variable named bestSport and initialize it to the first element of the sports array. (below is what they have given me)

String[] sports = {"basketball", "Baseball", "tennis"}:

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

4 Answers

Hiya,

You can declare the string with:

String bestSports;

But then you want to assign it the first element of the given array. You access those with the notiation, [index]. The array starts counting its indexes at zero.

The full declaration and initialisation will be:

String bestSports = sports[0];

I hope that makes sense.

Steve.

Hello Steve, thanks for your reply!

I have now entered String[] sports = { "Basketball", "Baseball", "Tennis" }; String bestSports; String bestSports = sports[0];

unfortunately I still have not managed to complete it?

Cheers Peter

Hiya,

I did that in two steps to illustrate.

You only need to declare the string once.

So either do

String bestSports;
bestSports = sports[0];  // <-- No 'String' preceding this

or in one line:

String bestSports = sports[0];

One declares then initialises - the second does both in one go.

Steve.

Hello Steve,

I restarted the challenge and did what you said and it worked!!

Thanks a lot for your help!