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

General Question

As we are trying to generate random fact what if we want to show facts in sequence ?? like on click 1st member of array then on 2nd click 2nd member and so on???

1 Answer

David Clausen
David Clausen
11,403 Points

Well instead of just grabbing a random index, grab the index and each time you display a fact add to that index

//pseudo code
OnClick()
fact = [index]
index ++

//if index goes over array 
if (index < array.length)
   index=0

I dont know what code your using, probably Java for this project. But this would solve it. I assume you know how to implement this if your this far.

Please Elaborate your answer or help me with this

public String[] mMixLetters ={"A","B","C"......................};

public String getColor(){
    String color = "";
    //Randomly select a fact
    Random randomGenerator= new Random();// construct a new random number generator
    int randomNumber = randomGenerator.nextInt(mMixLetters.length);//color.length is the length of index of array
    color = mMixLetters [randomNumber ];

    // fact = randomNumber + "";

    return color;

}

//that's the code and i want to get the array element in sequence not randomly how to modify this??