Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

Akash malhotra
Akash malhotra
3,025 Points

Android Fun facts multiple facts?

I'm currently working on creating my first basic app, where 3 fun facts are displayed. What would I do if i wanted like over a 100 fun facts? I feel like just typing each single line isn't an efficient way. Can I somehow make the facts in a txt document and somehow make android studio read the fiile and display the random facts? What can I do?

 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) {
    //button clicked, update with new fact
    String fact="";
    //randomly select fact
    Random randomGenerator= new Random();
    int randomNumber=randomGenerator.nextInt(3);

    if (randomNumber ==0){
        fact="Ants stretch when they wake up in the morning.";
    }
    else if (randomNumber ==1){
        fact="It is impossible to lick your elbow. ";
    }
   else if (randomNumber ==2){
        fact="Rats and horses can't vomit.";
    }

As you can see, I don't want to have 100 else if statements.

3 Answers

Lukas Smith
Lukas Smith
4,026 Points

You must prepare array of facts

     factsArray = [

    "tata marcina powiedzial ze piotrek to wybil szybe",

    "nie wiadomo co chodzi o chodzi o kase wiadomo",

    "czosnek smierdzi bardzo smierdzaco jak to czosnek",

    "Moszna, moszn azezrec bo jestemy glodne",

    "zlikwiduj moja bronzowa brode co :) likwidator bordy",

    "alibaba i 30 rozbojnikow, banda gejow pieprzona"

]

Next random your number

//randomly select fact Random randomGenerator= new Random(); int randomNumber=randomGenerator.nextInt(3);

and finally

fact = factsArray[randomNumber]

Akash malhotra
Akash malhotra
3,025 Points

So I would create an array with over 100 facts, all in strings right? Then in randomGenerator.nextInt(3) , i would have to change the 3 into the # of facts I have in order to randomize each one?

factsArray=["fact1","fact2","fact3,"fact4","fact5","fact6","fact7"]  
Random randomGenerator= new Random();  
 int randomNumber=randomGenerator.nextInt(8);  
fact=factsArray[randomNumber]  

So this would assign a specific random number to a spefific random fact in the array?

Lukas Smith
Lukas Smith
4,026 Points

Always try programming not hard code. So if You have array with 100 elements, You know is array from array[0] to array[99] yes? it is 100 elements. so my suggestion is int randomNumber=randomGenerator.nextInt(factsArray.count); Now when you add some facts more or delete one application knows how many facts is and will be no crash. because now if you try random number from 0 to 40 and try to access to factsArray[37] will be error

Akash malhotra
Akash malhotra
3,025 Points

What does factsArray.count do?

Lukas Smith
Lukas Smith
4,026 Points

Array.count return how many elements u have in array

Akash malhotra
Akash malhotra
3,025 Points

Isn't it array.length in Java?

Lukas Smith
Lukas Smith
4,026 Points

Could be i dont know Java