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)

Andrew D.
Andrew D.
2,937 Points

Third Fact(Silver Medals) Not Being Generated, Only First Two

Hey guys. I'll post my code below. For whatever reason the third fact about gold medals being comprised mostly of silver isn't being randomly generating, only the first two facts. I thought it had something to do with nextInt being set to 3 rather than 2(since it starts at 0 in Java) but my code is exactly like Ben's:

        public void onClick(View v) {
            //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);

            /* Convert the randomNumber to a text fact
            * 0 = Ants stretch when they wake up in the morning.
            * 1 = Ostriches can run faster than horses.
            * 2 = Olympic gold medals are actually made mostly of silver.
            */

            //if randomNumber is equal to 0, then set equal to ants fact
            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 of silver.";
            }
            else {
                fact = "Sorry! There was an error.";
            }

            factLabel.setText(fact);
        }
    };

Any ideas? Thank you!

Andrew D.
Andrew D.
2,937 Points

Disregard! I installed genymotion to get a faster emulator and now it works properly. But on the old emulator, I really did click 'Show another fun fact' about 20 times before I decided the code must not be working. It was probably a buggy emulator.