Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App (2014) Coding the Fun Facts Using Conditionals (else Statements)

Emil Wallgren
Emil Wallgren
11,737 Points

Doesnt 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 {

    protected void onCreate(Bundle savedInstanceState) {

        //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() {
            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

Got any ideas on what could be wrong?

Thanks! :-)


1 Answer

Ben Junya
Ben Junya
12,365 Points

Hmm, 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!