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

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

CD Lim
CD Lim
4,782 Points

cannot convert randomNumber into text fact

here is my code so far

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;

import java.util.Random;

public class FunFactsActivity extends AppCompatActivity {

@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 files
    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, it will update the with some new facts

            String fact = "";
            // Randomly select a fact

            Random randomGenerator = new Random(); //construct a new Random generator
            int randomNumber = randomGenerator.nextInt(3);

            /* convert randomNumber in a text fact
             * 0 = Ants stretch when they wake up in the morning
             * 1 = Ostriches can run faster than horses
             * 2 = Olympic Gold metals are usually made mostly of silver
             */
            // if randomNumber equals to zero then

            if (randomNumber == 0) {
                // set fact equals to ants fact
                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 metals are usually made mostly of silver";
            }
            else {
                fact = "Sorry there was an error";
            }

            // update the label with our dynamic fact
            factLabel.setText(fact);
        }

    };
    showFactButton.setOnClickListener(listener);


}