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 (retired 2014) Learning the Language Introduction to Arrays

Stuck on string variables again

I don't know why I can't seem to grasp this.

Declare a String variable named 'bestFrog' and initialize it to the first element of the frogNames array.

String[] frogNames = { "Mike", "John", "Todd"};

Hi Ashley,

What have you tried?

2 Answers

Daniel Hartin
Daniel Hartin
18,106 Points

Ok so declaring a variable is just telling android what you want you variable name and type to be. For this challenge you would put : String bestFrog;

Now the challenge says initialize it with the first element in the Array. Intializing a variable is just another name for making the variable name equal something, but what to equal?.

An array is a collection of items in this case strings which is what we want. The way we obtain any value from an array is to use a number which references the position of the item we wish to extract. In this case it is 0 as arrays start with a position number of 0.

Okay with everything above joined together we should type the following:

String bestFrog = frogNames[0];

Hope this helps Daniel

Hi Ashley,

I believe Daniel has already given you a well explained answer for the first part. I recently replied to a similar question in this code challenge and posting the same here. If however, you have any doubts you may ask again.

The explanation is for the 2nd and third part of this challenge I reckon, though I would ask you to give them a go and then look at the solution and explanation here

Hope that helps

******************************* Excerpt from my previous answer *******************************************

From the code provided and have had looked at the challenge this is what you would need to do

Declare a string variable called lastSport and then assign it to the last or 3rd element (index 2) of the array i.e.

String lastSport = sports[2]; //since index 2 or element no 3 or Tennis is the last sport in the array

This should have been easy since you have already defined the first sport in the array with index [0]

Alternatively a second better way to do this challenge

String[] sports = { "Basketball", "Baseball", "Tennis" };
String bestSport = sports[0];
// int numberOfSports = (sports.length);    ... No need to put sports.length in brackets
int numberOfSports = sports.length;
String lastSport = sports[numberOfSports-1];

Reason : sports.length will give the value 3 since there are three elements. This value is now stored within numberOfSports

Now since index numbers start from 0 so the last value in an array will by convention be length of array - 1

Array