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

Stuck on challenge 3 of 3 Coding the Fun Facts, Build a simple android app

This challenge wants me to declare a String variable named lastSport and initialize it to the last element of my sports array.

this is my code so far... if i assign it as the last element via [3] i get outOfboundsException 3().

String[] sports = { "Basketball", "Baseball", "Tennis" };
String bestSport = sports[0];
int numberOfSports = sports.length;
String lastSport = sports[3];

After some research on stack exchange i found arrayCharToInt.length - 1 but i dont know where i need to insert this. or what to do with it if ineed to make an if loop to check equality or...? any help would be great. thanks TeamTreeHouse community. =)

2 Answers

Spoiler: The way I solved it, was to use the numberOfSports, and used the last way to find how many -1 to find the last number in a zero based counting system.

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

Jonas Schindler
Jonas Schindler
4,085 Points

That is because the index [3] doesn't exist. Programming is "zero based" meaning you start counting from 0 instead of 1. If you want to access the third number of the array write sports[2]

Doh.... I KNOW THIS. LOL thank you so much jonas. I guess I need to keep pounding Programming is zero based counting in my head.

You know its time to take a break when I'm forgetting something as simple as this.