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 trialEmil Wallgren
11,737 PointsDoesnt convert to number in if-statement
Hi! I'm this lesson and believe to have written everything correct after multiple checks...Still the emulator produces numbers in stead of the sentences the numbers should correspond to. My code looks like this:
public class FunFactsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
//Declare our View Variables and Assign the Views from the layout file
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton = (Button) findViewById(R.id.showFactButton);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
//The Button was Clicked, so update the fact label with a new fact
String fact = "";
//Randomly Select a fact
Random randomGenerator = new Random(); //Construct a new random number generator
int randomNumber = randomGenerator.nextInt(3);
if (randomNumber == 0){
fact = "Ants stretch when they wake up in the morning";
}
else if (randomNumber == 1){
fact = "Ostriches can run faster than horses";
}
else if (randomNumber == 2){
fact = "Olympic gold medals are actually made mostly out of silver";
}
else {
fact = "Sorry, there was an error :-(";
}
//Update the label with our dynamic fact
factLabel.setText(fact);
}
};
showFactButton.setOnClickListener(listener);
}
Got any ideas on what could be wrong?
Thanks! :-)
/Emil
1 Answer
Ben Junya
12,365 PointsHmm, it looks like everything is ok, just from examining your code...
I know you're a beginner, but this is an integral part of programming. You'll learn more about it in the next couple of lessons, but consider this a sneak peek of what's to come.
I want to know if your string is being correctly assigned to the text. Do this:
Before factLabel.setText(fact); add these lines:
Log.i("TEST LOG", "fact is " + fact); Log.i("TEST LOG", "RANDOM NUMBER IS " + randomNumber);
After entering in these lines, you'll also need to organize your imports.
Run your program, then click the button and look in LogCat in the Android window of Android Studio. You should see:
TEST LOG fact is (your fact) TEST LOG RANDOM NUMBER IS (randomNumber)
This should give you some clues into what's going on. Let me know what's happening in your code. Let me know if you can figure it out from here. Using log statements are a great way to interrogate your app and find out what's going on, or what's going wrong.
Good luck! Reply here if you need more help!