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 trialVladimir Kreckovic
5,252 PointsNeed 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
6 Answers
James Simshaw
28,738 PointsHello,
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
8,079 PointsArray 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.
MUZ140698 Kudakwashe Gwaindepi
4,150 Pointsthanks so much Robin, you saved me
Vladimir Kreckovic
5,252 PointsString[] 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
5,252 PointsThank you James for your help! I've managed to sort it out following your advice.
Thanks again!
James Simshaw
28,738 PointsGlad I was able to help. Good luck with Android programming.
Salomon Alcoba Trujillo
Courses Plus Student 1,056 Pointsthis 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
3,584 PointsGood advice James Simshaw! Thank you!
James Simshaw
28,738 PointsJames Simshaw
28,738 PointsDo you have any code that you have tried for this? Without seeing your code, I cannot tell you what is going wrong.