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 trialMark Fremer
Courses Plus Student 171 PointsI'm sure I answered the question correctly am I missing something?
I am in: Build a Simple Android App > Learning the Language > Introduction to Arrays
Question is: Declare an int variable named 'numberOfFrogs' and set it to the number of elements in the array using the array's 'length' property.
This is what i did, Would I be missing something?
String[] frogNames = { "frog1", "frog2", "frog3" }; String bestFrog = "";
int numberOfFrogs = randomGenerator.nextInt(frogNames.3);
bestFrog = frogNames[numberOfFrogs];
I also tried
String[] frogNames = { "frog1", "frog2", "frog3" }; String bestFrog = "";
int numberOfFrogs = randomGenerator.nextInt(3);
bestFrog = Integer.toString(numberOfFrogs);
I am pretty sure i am close.
12 Answers
samiff
31,206 PointsHey Christopher, if you're doing the challenge Introduction to Arrays, it seems like either the question content has changed (it's been awhile since the thread started), or things got mixed-up along the way because there is no need to use the random number generator for this challenge.
String[] frogNames = {"Mike", "Sam", "Joe"};
String bestFrog = frogNames[0];
int numberOfFrogs = frogNames.length;
String lastFrog = frogNames[2];
Adam Davies
1,034 PointsRight, I have figured out why it is saying it is wrong. The question hasn't told us to change the result of best frog to the number of frogs.
The code should be.....
String[] = frogNames { "Barry", "George", "Timmy"};
//Answer to this question
int numberOfFrogs = frogNames.length;
//Answer from the previous question below because the question hasn't asked us to change this
bestFrog = frogNames[0];
The randomNumber doesn't even get mentioned in any of the questions so it should't be included in any of the answers.
Hope this helps anyone who is stuck. It seems the rule of thumb is to alter your answer to exactly what the question is asking not necessarily what should be the complete result of the code.
Adam Cowley
5,472 PointsHi Mark,
To find the length of an array you should use the .length property, like the following:
// Declare array of frogs
String[] frogNames = {"Frog1, "Frog 2","Frog 3"};
// Get total number of frogs
int numberOfFrogs = frogNames.length;
Adam Cowley
5,472 PointsMark, as far as I can see you haven't defined the randomGenerator variable. You're both looking at something along the following lines
String bestFrog = "";
String[] frogNames = { "George", "Barry", "Timmy", };
// Total Frogs
int NumberOfFrogs = frogNames.length;
// Get Random Number
Random randomGenerator = new Random;
int randomNumber = randomGenerator.nextInt(NumberOfFrogs)
// Set variable
bestFrog = frogNames[randomNumber];
Hope this helps
Charles Hill
Courses Plus Student 281 PointsIn this frog lesson we are not looking for a random frog just a list of frogs
Mark Fremer
Courses Plus Student 171 PointsYes that was what I was doing then I added the following to finish the string
String bestFrog = "";
bestFrog = frogNames [randomNumber];
and it is not accepting my answer.
In full for my answer i have
String bestFrog = "";
String[] frogNames = { "frog1", "frog2", "frog3" };
int numberOfFrogs = randomGenerator.nextInt(frogNames.length);
bestFrog = frogNames[numberOfFrogs];
and also tried
String bestFrog = "";
String[] frogNames = { "frog1", "frog2", "frog3" };
int numberOfFrogs = frogNames.length;
bestFrog = frogNames[numberOfFrogs];
Adam Davies
1,034 PointsHi
Im stuck on this same task. Have you gotten any further with it. My Code is
String[] frogNames = { "George", "Barry", "Timmy", };
int numberOfFrogs = frogNames.length; String bestFrog = frogNames[numberOfFrogs];
I can't see where I'm going wrong.
Christopher Kodadek
Courses Plus Student 1,011 PointsWTF. Does any1 have the solution to this?
Adam Davies
1,034 PointsHi guys.
Anyone who is having trouble with this question just look at my explanation above.
Regards
Christopher Ballenger
290 PointsThanks Adam Davies! I see the problem, for some reason steps 2 & 3 are switched around. Shame. 3 needs to be done before 2, so look at Adam Davies code.
Brandon C
3,091 PointsSadly I was stuck because the challenge never notified or said anything about what to input into the array.
adding
bestFrog = frogNames[0];
is all it took.
I had everything right except the frogNames[0];
annoying,
Jose Beltran
1,195 PointsYou got to add String to bestFrog like this String bestFrog = frognames[0];