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 Introduction to Arrays

Hi, my Fun Facts App is showing the numbers 0, 1, 2 instead of the Fun Facts that were coded in.

The first fact shows up when the app starts up, then when I press the button to show another Random Fact, it shows the number 0, 1 and 2 instead.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Post your code and we'll try to help troubleshoot. :)

package com.example.slimjim.funfacts;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
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 FunFactActivity extends Activity {

    @Override // used to separate methods
    protected void onCreate(Bundle savedInstanceState) { // void is a place holder  which let
    // let the compiler no you returning nothing
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fun_fact);

        //Declare our View Variables and assign the View label 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() { //listener is the variable for
            //on click lister function
            @Override // prewritten once a new variable was created for on click variable
            public void onClick(View view) {
                // the button was clicked, so update the fact label with a new fact

                String[] facts = {
                        "Ants stretch when they wake up in the morning.",
                        "Ostriches can run faster than horses.",
                        "Olympic gold medals are actually made mostly of silver.",
                        "You are born with 300 bones; by the time you are an adult you will have 206.",
                        "It takes about 8 minutes for light from the Sun to reach Earth.",
                        "Some bamboo plants can grow almost a meter in just one day.",
                        "The state of Florida is bigger than England.",
                        "Some penguins can leap 2-3 meters out of the water.",
                        "On average, it takes 66 days to form a new habit.",
                        "Mammoths still walked the earth when the Great Pyramid was being built." };


                String fact = "";
                // Randomly select a fact
                Random randomGenerator = new Random();//construct a new random number generator
                int randomNumber = randomGenerator.nextInt(facts.length) ;
                fact = randomNumber + "";// added + and "" to combine numbers and string
                //update the label with our dymanic fact

                //convert the random number to 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 actually made mostly of silver"
                //if randomNumber equals 0
                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 metals are actually made mostly of silver";
                }





                factLabel.setText(fact);




            }
        };// a semicolon had to be added here
        showFactButton.setOnClickListener(listener);// you had to add the declare variable here to
        //listener inside () to remove function


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.fun_fact, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

I am having the same problem my code is displaying the number in the array and not the string what am I doing wrong can you help me

aneesh kumar
aneesh kumar
1,426 Points

try deleting this fact = randomNumber + "";

Jeroen de Vrind
Jeroen de Vrind
29,772 Points

Amon Dow III when i run your code i got the facts displayed for numbers 0, 1 en 2 and 3-9 are shown as numbers. That's because they're not in the if-statements. Maybe Clean and Rebuild (in the Build menu) will help?

3 Answers

I think it might possibly be caused by what you have in your onClick method. Do you make array of facts appear on the screen or just the random number?

Shane Desouza
Shane Desouza
3,523 Points

Did you link the String fact to display the facts? I think it might be caused because you haven't linked it.

I'm having the same problem