Choosing a Random Fact4:41 with Ben Deitch
It's often helpful to group items together into an array. In this video, we'll create an array of facts. Then we'll use our randomNumber to help us pick a random fact!
Facts (for copy/paste):
val facts = arrayOf( "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.")
We just programmed the heart of our app, the random number generator. 0:00 Now we need to use that generator to help us pick a random fact 0:04 on each button click. 0:08 But before we can do that, we'll need a pool of facts to choose from. 0:09 Let's start by creating an array to hold our facts. 0:14 Remember, an array is just a container that holds values of a specific type. 0:17 Let's add two lines to the top of our OnClickListener, and 0:22 then create a new val named facts. 0:26 Now, for the assignment side of things. 0:31 For the sake of time, I'm going to paste in ten facts and 0:32 then we'll go over the syntax. 0:35 You can type these out if you want, but 0:37 I've put this code in the teacher's notes so you can just copy and paste as well. 0:39 Let's do it, I'll select everything after the word facts, then I'll 0:43 copy this and paste it after our facts variable back in the OnClickListener. 0:49 And now we can see that one way to initialize an array in Colin, 0:56 is just to type arrayOf, and then add each item as a parameter. 1:00 Now that we have an array of strings to select from, 1:07 let's use our random number generator to help us pick one at random. 1:09 Our array has ten elements in it, lets start 1:14 by changing our random number generator to have a range of 10 instead of 3. 1:17 If we clicked on our button now, we'd see random numbers ranging from 0 to 9. 1:24 And what else do we have that arranges from 0 to 9? 1:29 That's right, the indices of our facts array. 1:33 This first fact about ants is at index 0, and 1:37 this last fact about mammoths is at index 9. 1:40 So if we want to select a random fact, we just need to select the fact 1:45 from our facts array that's at the index of the random number we generated. 1:49 Seems easy enough, 1:54 let's start by getting rid of what we're currently setting our fact to. 1:55 And then type, facts [randomNumber] and 2:01 make sure you have a right bracket to close it off. 2:06 This selects the fact at the index of our random number. 2:13 All right, that should do it, 2:17 but before we test our app, there's a potential bug we should fix first. 2:19 What happens if we add a new fact to the array? 2:24 Can you tell where the code will break? 2:27 I'll add another fact and then let's talk about it. 2:29 I'll say, Treehouse is not actually in a tree. 2:32 Now that we have 11 elements in our array, we need to change 10 to 11 down here, 2:42 and our call to nextInt. 2:47 But if we forget to change it, we'll never see our new fact. 2:49 Wouldn't it be nice if we could somehow use the number of elements in the array 2:53 as our parameter, instead of a hard coded number like this? 2:57 Then we wouldn't have to change it every time we add or 3:01 remove elements in our array. 3:03 Sure enough, Colin has a way for us to do this. 3:06 Let's delete the number 10, and type facts.size. 3:09 This gives us the size of the facts array, 3:15 which is another way to say the total number of elements. 3:18 By using this property of the array, 3:22 we will always pass in the right number of elements. 3:24 This saves us the work of manually changing numbers, and 3:27 it also protects us from errors. 3:30 For example, if we had deleted an element, I'll delete this one that I just added 3:32 But we left this number as 11, then our app might try to reference 3:39 an element that doesn't exist, and it would crash. 3:44 Looks good to me, let's click on the run button to test our app. 3:48 Then we can tap the button, and we should see some of the new facts. 3:56 And there we go, we got something new. 4:01 Olympic gold medals are actually made mostly of silver? 4:03 And if we keep clicking, we should eventually see all of our facts, 4:07 very cool! 4:10 Awesome work, don't worry if all these new Android words aren't sticking yet. 4:14 We've done a lot, and we still have a lot to do. 4:19 But if you stick with it, I'll explain everything you need to know. 4:22 And if you wanna talk about anything you're stuck on, 4:25 head over to the community for questions or discussions. 4:28 It's easy to feel overwhelmed with this stuff, but 4:31 trust me, things will start to make more sense the more you practice. 4:33 On that note, why don't you try some more practice? 4:38
You need to sign up for Treehouse in order to download course files.Sign up