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
Introduction to Arrays code challenge
I must be missing something from the video on intro to arrays. i'm doing the code challenge and i'm on task 2 of 4. right now i have to Declare a String variable named ‘bestFrog’ and initialize it to the first element of the frogNames array. So far this is what i have:
String[] frogNames = { "Mike", "Ben", "Harry"};
string bestFrog = "";
could somebody help me please?
11 Answers
Ben Jakuben
Treehouse TeacherIf you are receiving a "Compilation Error", it's probably because "String" should be capitalized in the second line. String is a full-fledged Object in Java, and the convention is to capitalize all object data types. Other simple, or primitive, data types like int and boolean are not full-fledged objects, which is why they are lowercase. (Side note: There are full-fledged Object "wrapper" classes like "Integer" that can be used to wrap around primitive data.)
Anyhow, even fixing that won't pass the task for you. Right now your variable is being initialized to an empty String: "". To initialize it to the first element of the array, you'll need to change the empty String to the code needed to get item 0 of the frogNames array. (Since arrays start with zero, the first item is actually item 0.)
Hope this helps!
Creede Lambard
8,798 PointsIf you run
String bestFrog = "0";
you are literally setting the value of your String variable bestFrog to "0" - the character 0, not even the number 0. That's not what you want.
What you want is to set bestFrog to the first String in the array of Strings you declared for frogNames. For some reason programmers think 0 is the first number in an array*, so you want to set the value of bestFrog to element 0 of frogNames.
Here's a hint: If you wanted to set it to the third element of frogNames, you'd do
String bestFrog = frogNames[2];
and then if you were to print bestFrog to the screen you'd see "Harry".
Good luck!
- there's actually a very good reason why they think that.
Ben Jakuben
Treehouse TeacherHi @Mark,
It looks like your syntax might be off: frogNames String[] bestFrog = { "Mark", "Mike", "John" }; will give you a Compilation Error. So step 1 is to fix your code:
- Line 1 declares a String array named frogNames (i.e. String[] frogNames ...)
- Line 2 declares a String named bestFrog (i.e String bestFrog ...)
Now, the code challenge task is asking for the first item in the array. Arrays start with an index of zero, so to get the first item, you need to use the correct index. The index of 1 actually gives you the second item.
Ben Jakuben
Treehouse TeacherYou are so close! It can be so hard to get the syntax just right sometimes. Compare this line from Creede's response with yours:
String bestFrog = frogNames[2];
String bestFrog = "frogNames 1";
Creede's shows you how to get the 3rd element. Now if you're wondering why the 3rd element is number 2, that's because we're starting counting at zero with arrays. So for the first element we'll use 0, for the 2nd we'll use 1, and for the 3rd we'll use 2.
Every character, even the double quotes, the semicolon, and the capitalization, need to be exactly right. So take Creede's line and just make the one change required to get the 1st element instead of the 3rd.
Hope this does it for you!
ok, so i capitalized the string in the second line and now i have it as this:
String bestFrog = "0";
but its giving me an error: Make sure that bestFrog is the first element in the frogNames array. i've already tried every combination i can think of, butting it above the array and as the first line after i start the array and before the first name, i put it after the array ends, I'm just at a loss. but what you said did make sense to me, cant believed i missed that.
Ben Jakuben
Treehouse TeacherThanks for answering, Creede! Also, for anyone interested in your notation about array's starting at zero for good reason, here's a decent programmers.stackexchange.com answer about that.
i feel so stupid right now. i swear everything looks right but its telling me to make sure that bestFrog is set to the first frog name. i've got it in there. String[] frogNames = { "Mike", "Ben", "Eddie"}; String bestFrog = "frogNames 1";
and i've even done it without putting frogNames in there and just having "1", i've watched the tutorial video more than once and i'm still not getting it.
i cant believe that i missed that. thank you so much. its really awesome that the guy teaching the material is on here helping out. it makes this site stand out from everything else.
Ben Jakuben
Treehouse TeacherAwesome! Glad you got it, and glad I could help! Stop back in the forum if you have any other trouble or questions. :)
Mark McAninch
768 PointsfrogNames String[] bestFrog = { "Mark", "Mike", "John" }; String bestFrog = frogNames [1]; Where is the problem?
Justin Thorson
1,595 PointsThe very first line of your code is incorrect. frogNames was not yet established and should go after the 'String[]' array. It should look more like this:
String[] frogNames = [ "Mark", "Mike", "John"}; String bestFrog = frogNames[0]; *Remember that the number '1' would actually be calling the second variable in your array, and '0' would be the first.
Stormy Forrester
1,360 PointsStormy Forrester
1,360 PointsI've been stuck on this question far too long. Please just update an answer or update your video.
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherSorry to hear that! We added compilation errors to the challenge a while back. Start a new thread and paste your code in and we'll help you get past it. Also, I just released a new version of this course yesterday, so you may be interested in that.