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

Jorge Flores
Jorge Flores
7,864 Points

how to get the last element of an array?

I am using the numberOfSports variable to get the last element of an array like this:

String lastSport = sports [numberOfSports-1];

I'm also staticly writing down the last index in the array:

String lastSport = sports [3];

Why are both answers being marked as wrong?

Array.java
String bestSport = "soccer";
String[] sports = { bestSport,"Basketball", "Baseball", "Tennis" };
int numberOfSports = sports.length;
String lastSport = sports [numberOfSports-1];

6 Answers

Harry James
Harry James
14,780 Points

Hey Jorge!

I'll try to explain each part of the challenge in an easier way to understand, sometimes the wording can confuse students:

For the first part of the challenge, you are wanted to set bestSport to whatever is at index 0 in the sports array. Note that you will need to do this after the bestSport variable has been initialized.

For the second part of the challenge, you want to set numberOfSports to the length of the sports array. To do this, you should use the length() method off the sports array.

For the final part of the challenge, you want to set lastSport to whatever is at index 0 in the sports array. To do this, you should use the numberOfSports variable. This will give you a number starting from 1, of how long the array is. As we're working in indexes, we want to start from 0 and we can do this by taking 1 away from numberOfSports.

Note that in this challenge, there isn't any specific ruling and in fact, you could cheat a bit and just set the bestSport variable to "Basketball", like you originally did. This will still pass the challenge but it's best practise to try and do it the proper way ;)


Hope it helps and good luck with the challenge! If you get stuck along the way or if there's something you want clarified, feel free to give me a shout :)

Oh I didn't actually check the challenge, sorry. Hmm I have no idea where that soccer came from. But with this code I passed the challenge without any problem:

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

String bestSport = sports[0];
int numberOfSports = sports.length;
String lastSport = sports[numberOfSports - 1];

That is the way to get it. You can compress these two lines:

int numberOfSports = sports.length;
String lastSport = sports [numberOfSports-1];

to a single line:

String lastSport = sports[sports.length - 1];
Jason Butler
Jason Butler
5,732 Points

It might also be an issue with having a space before the bracket; ie, rather than sports [3] it should be sports[3]. And as Kristian wrote, sports[sports.length - 1].

Jorge Flores
Jorge Flores
7,864 Points

I already tried both of your solutions, but they still reject them :(

Jorge Flores
Jorge Flores
7,864 Points

Thank you so much to everyone, I finally solved it out with all your help. My mistake was that in the first exercise, I understood that I should declare a new string with another sport and put it at the beginning of the array. For some reason it was marked as correct, but at the end in the 3rd exercise, the error finally came out.