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 with Java Testing and Debugging Wrapping Up

Mark Ji
Mark Ji
675 Points

Some improvements

For this app, how can i make the facts appear in sequence I want instead of randomly generated ?

2 Answers

Luke Strama
Luke Strama
6,928 Points

Once you have the facts in the order that you want them, just get it to run through the index using a 'number' variable and adding 1 to it. Once you get to the last value, it restarts to index 0 and goes again. Not setting it back to 0 crashes the app because it runs out of elements in the array.

I left the random version commented out for reference.

public class FactBook {
    // Fields or Member Variables - Properties about the object
    private 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." };

    // Methods - Actions the object can take

    private int number = 0;

    public String getFact() {
        // Randomly select a fact
        /*Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(facts.length);
        return facts[randomNumber];
        */
        if (number < facts.length-1) {
            number++;
        }
        else {
            number = 0;
        }
        return facts[number];
    }
}
Mark Ji
Mark Ji
675 Points

thx a lot! I was thinking about adding a counter in the "mainactivity" class, but found the value fail to update each time I click the button. Defining a private counter in "Factbook" class is clear and reasonable !

Mohammed Safiulla D
Mohammed Safiulla D
17,044 Points

is not just "if (number < facts.length)" enough? I don't think you need a -1 as you can never see the last fact.

Armin Halilovic
Armin Halilovic
3,372 Points

Here one explanation for why using facts.length-1 and not just facts.length (Better late than never)

Because arrays use a zero-based index, to return the last element of an array we have to use facts.lenght-1. Here an example:

Lets say we have an array of 5 numbers: int[] numbers = {1,2,3,4,5};

Now, to get the last element of the array (in this case the 5) we need to print the array (numbers) and the index that we want to print: System.out.println(numbers[4]); // index 0=1, index 1=2, index 2=3, index 3=4, index 4=5 Output: 5

This means the size of the array is 5, but the last element has index 4. And the same applies to the facts array: The array has the size of facts.length but the last element has index facts.length - 1