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 trialchandlerknaus
1,635 PointsStrings named 'titles'
I am stuck on Challenge 2/4 For the first challenge I did
String title1 = "Android is awesome"; String title2 = "Mike is cool"; String title3 = "Treehouse loves me";
Now I am stuck on making the strings an array. I am probably for getting something silly but can someone please help me?
David Hope
19,876 PointsThere are 2 different ways to create a String Array Chandler. When doing so directly in your Java code, add square brackets [] directly after you declare the data type(String) and right before you give the array a name. You will then set it using an = sign and add each String you wish for it to hold inside the curly braces, surrounded by double quotes and separated with a comma, except for the last one.
The second way, which is better for a variety of reasons, is to declare your String array in the strings.xml file using the string-array type, give it an appropriate name, and declare each string(no double quotes) as an item in the array like so <item>String for array here</item> You'll then reference the string array first by creating a String array member variable, give it a name, but no value. Then you will create a new Resources object inside the onCreate method, instantiate it and use the getResources method for its value. On the following line you will then use the member variable you created and set its value with the equals sign to the name you gave your Resources object along with do notation in order to use the getStringArray method. For the getStringArray method you will use R.array and one more dot with the name you gave the string array in the strings.xml file.
I hope this helps and good luck with the rest of this fun project Chandler!
4 Answers
Gloria Dwomoh
13,116 PointsThat is because it asks you to Initialize it with the three variables you have created. What you did is technically right, but not what it asks. It asks for this
String[] titles = {title1, title2, title3};
Gloria Dwomoh
13,116 PointsHere is an example of how to make a string array.
String[] flowers = {"Cherry blossoms", "Tiger lilies", "Roses"};
I hope my flowery example helped :D
chandlerknaus
1,635 PointsString[] titles = {"Android is awesome", "Mike is cool", "Treehouse loves me"};
When I did this I got a compiler error and it didnt work O_o
chandlerknaus
1,635 PointsOooohhhh ok thank you
Gloria Dwomoh
13,116 PointsYou are welcome.
Gloria Dwomoh
13,116 PointsGloria Dwomoh
13,116 PointsWhat code did you write to make a string array?
Ps. I hope you added the brackets.