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 Properties of Arrays

Vladimir Kreckovic
Vladimir Kreckovic
5,252 Points

Need help for code challenge...

Hi,

I need little help for code challenge in "Properties of Arrays". I can't figure out what I am doing wrong. Challenge task is:

"Declare an int variable named numberOfSports and set it to the number of elements in the array using the array's length property."

Thanks for help

Do you have any code that you have tried for this? Without seeing your code, I cannot tell you what is going wrong.

6 Answers

Hello,

For this challenge, you don't need to do anything with random numbers. All this Task is asking is to declare an int variable named numberOfSports and set it to the number of elements in the array.

Right now, you have declared numberOfSports as a String, when the Challenge was asking for it to be an int. Then you would just assign that variable the value of sports.length, you can remove all of the Random stuff as well as the return statement since we're not in a function that is expecting you to return something.

Robin Larsson
Robin Larsson
8,079 Points

Array have a property called 'length'. It will return how many elements that's in the array. So if you have an array: String[] words = {"hello", "world"}; You can use the length property like this: words.length; This will return 2, because we have 2 elements in the array.

thanks so much Robin, you saved me

Vladimir Kreckovic
Vladimir Kreckovic
5,252 Points

String[] sports = { "Basketball", "Baseball", "Tennis" }; String bestSport = "Basketball"; String numberOfSports = ""; Random randomGenerator = new Random(); int randomNumber = randomGenerator.nextInt(sports.length); numberOfSports = sports[randomNumber]; return numberOfSports;

This is one of the codes that I've tried....

Vladimir Kreckovic
Vladimir Kreckovic
5,252 Points

Thank you James for your help! I've managed to sort it out following your advice.

Thanks again!

Glad I was able to help. Good luck with Android programming.

Salomon Alcoba Trujillo
PLUS
Salomon Alcoba Trujillo
Courses Plus Student 1,056 Points

this was the answers

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

i was writing wrong the numberofSports should be numberOfSports trying stupid things and the answer was the variables

Predan Alexandru
Predan Alexandru
3,584 Points

Good advice James Simshaw! Thank you!