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

Fun Facts app?

So below is my code for my fun facts app. What I would like it to do is go from 1-3 in order and not be random like I did below BUT I am not sure how to make it so its like the "getfact" on the original code. I know I have to make the getfact again but I am not sure how to make it so it is not random and pulls the facts from the code I wrote. Sorry if this is confusing!

ORIGINAL CODE

    public String getfact() {
        String fact = "";
        //Randomly select a fact
        Random randomGenerator = new Random();  // Construct a new random generator
        int randomNumber = randomGenerator.nextInt(mFacts.length);

        fact = mFacts [randomNumber];

        return fact;
    }

}
 public class Facts {

     public static void main(String[] args) {

         // Create int array.
         int[] array = new int[3];

         // Assign first three elements.
         array[0] = Integer.parseInt("Ants stretch when they wake up in the morning.");
         array[1] = Integer.parseInt("Ostriches can run faster than horses.");
         array[2] = Integer.parseInt("Olympic gold medals are actually made mostly of silver.");

         // Loop over elements.
         int i = 0;
         while (i < array.length) {
             // Get value.
             int value = array[i];
             // Print value.
             System.out.println(value);
             i++;
         }
     }

2 Answers

I think this would not work for Android because it uses the console (stdout - println) and this isn't included in android. Next problem is that you are declaring integer variable (int value) and are using String as its value (from the array). There is also a difference to the FunFacts App: the facts would be shown in a row and at the same time, not when you click the button.

If I would like the facts to be in a order then I would write the getFact() method e.g. like this:

public String getFact(){

int i = 0;
String fact = "";
fact = mFacts[i];
i++;
if(i>=2){ //or better mFacts.length --> end of the array
i=0; //beginns with the first fact
}
return fact;
}

I hope this helps you (and is understandable), if not then just add a comment this answer.

Sorry I took that code from a different project from awhile ago.. still dont know a lot about android. Ok so I did what you suggested but I am not sure where I would put the facts?

Sorry I took that code from a different project from awhile ago.. still dont know a lot about android. Ok so I did what you suggested but I am not sure where I would put the facts?

Nvm I am so stupid sorry

So did you solve it? The beginnings aren't that hard, you just need to understand few basic concepts, so I'd recommend you to go through the whole Fun Fact App course and then you'll know how to do that in order.

Yea I got it thank you! I have to sell my computer so I paused my membership until I get a mac.