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
marcus purcell-johnson
1,472 PointsAll about Strings
i am confused on this part. (task 3 of 5) Can someone please explain this for Me?
Set the text of the Button using the 'label' variable instead of the hard-coded text currently there.
Ernest Grzybowski
Treehouse Project ReviewerCan you post the link that this question is in reference to?
5 Answers
Ernest Grzybowski
Treehouse Project ReviewerOn part 3 of the code challenge it states:
Set the text of the Button using the 'label' variable instead of the hard-coded text currently there.
It wants you to replace the hard coded string with a variable in the setText() method with a variable.
Before:
myButton.setText("Replace the text in here");
Your end result would be something like this:
After
String myName = getString(R.id.myAwesomeName);
saveButton.setText(myName);
That should help you get past step 3.
marcus purcell-johnson
1,472 Pointsthanks for the help Ernest. i apologize for not sending the link but i needed to take a break from My computer. thanks for explaining this to Me.
Ben Jakuben
Treehouse TeacherGreat answer, Ernest! I replied to Marcus through our help desk before seeing this post here, so I'm going to paste in my response in case it helps anyone else who visits this question.
So the idea for task 3 in this challenge is to use the String value from a variable instead of the hard-coded text that we're currently using. In this case, the hard-coded value is "SAVE", and we want to replace that with the value in the label variable.
If you haven't heard the term "hard-coded" before - that means that in the code we have a hard value like a String or number instead of a variable value which is used with a variable placeholder. Hard values are "hard" in that they can't be changed unless a programmer goes in and types a new value. Variables can be changed in a number of ways, like input from a user, for example.
The reason we do this is so that we can update the variable in one place, and then everywhere that references that variable will also be updated. In this code challenge example the variable will only apply to the saveButton, but we can imagine an app that has a number of different save buttons, and instead of setting each individually we can just set one variable and use that instead for all the buttons.
Ben Jakuben
Treehouse TeacherMarcus,
I'm sorry you are still stuck! I thought we had enough information for you to move on. I was travelling yesterday and just now got the latest messages. I'm going to paste the code from your screenshot in here to reference:
String[] phoneNumberLabels = { "-home", "-work", "-mobile" };
Button saveButton = (Button) findViewById(R.id.saveButton);
String label = getString(R.string.saveLabel);
String label = getstring(R.id.saveButton);
saveButton.setText(label);
Short answer: Just remove that second line from the bottom. Now for the explanation.
The line above declares a variable named 'label'. It says that it's a String data type, and then you use the equals sign to put a value in it. In this case you get a String stored in the Strings resource file (strings.xml), using a special method called getString().
So your second line is invalid because you are trying to declare another variable named 'label'. Declaring a variable means that you type the data type and then the name. It can be as simple as the following:
int someNumber;
When you declare a variable, it means that name is taken, so you can't declare it again, which is what your code is trying to do. The following would be illegal:
int someNumber;
int someNumber = 10;
You can set the variable anywhere else as long as you don't put a data type like String or int in front of it. Here's the fix for that illegal example:
int someNumber;
someNumber = 10; // The someNumber variable now holds the value of 10
Likewise, you can't change the datatype of a variable. So the following is also illegal:
int someNumber;
String someNumber;
There are a few other problems with that line of code, too. Your call to getString() has a lowercase s where uppercase is needed, and you pass in R.id.saveButton, which is the reference used to get a Button object. R.string is used to get String resources.
All of those issues are easier to troubleshoot in a real IDE like Eclipse, because you get way more helpful error messages. Hopefully we can iterate on our code challenges in the future to make them better like that.
When all else fails, walk through your code line by line to make sure you understand how it's operating. If you get to a line where you start guessing or don't know how it's working, then it's time to ask about it here or in other places like StackOverflow.com. I know this is easier said than done, but we're here to help, so stick with it!
Sanat Tripathi
8,109 PointsErnest Grzybowski : Hey even I said the same thing and I didn't get any upvotes and neither any points :(
marcus purcell-johnson
1,472 PointsMy fault bossman (Mr. Sanatkumar). thank you too. its just that Ernest's answer was more comprehendible for Me. Your answer was explaining it to Me as if I understood what you were saying. I am new to coding and I didn't understand your answer exactly. But I do want to thank you for taking the time to give Me the right answer, whether I understood it or not.
marcus purcell-johnson
1,472 Pointsthanks for the help guys, but this is beginning to annoy Me, because i am typing in everything how i was told in addition to various other equations and i keep getting the 'Compilation Error' message, yet the test doesnt highlight what is wrong. so i keep making guesses. i have been stuck on this one question for 2 days now, i have watched the video about 10 times, googled and youtubed results but i just cant figure this out. i may laugh at this one day but right now i seriously need this broken to me down in 'dummy terms'.
Rajesh Mule
2,125 PointsString[] phoneNumberLabels = { "-home", "-work", "-mobile" };
Button saveButton = (Button) findViewById(R.id.saveButton); String label = getString(R.string.saveLabel);
saveButton.setText(label);
Sanat Tripathi
8,109 PointsSanat Tripathi
8,109 PointsI don't remember that task now, however if I take a guess, they might be asking you to set the text of the button using the "setText()" method. So jus get a reference of the button widget (which might be the "label" variable as you mentioned in the question) and call .setText("With an appropriate name here");