Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Multiple Strings4:34 with Craig Dennis
In this video, we will expand on what we've learned to allow to prompt for multiple variables and use them in a single formatted String.
[MUSIC] 0:00 Now that we know how to take input and output it into our formatted text, 0:04 we're ready to get started on this MadLibs like program. 0:08 By the end of the stage, we'll have a working prototype. 0:12 First, let's review the main concept of this program. 0:16 The way Mad Libs works in real life is two people get together and 0:19 one of them has a book. 0:23 Each page of the book has a story that has blank lines that have types of words 0:24 such as adjective, noun, verb, etc. 0:29 The narrator asks the person for each of those types of words and 0:32 fills in the blanks with the word that was suggested by the other player. 0:35 The story is then read with the new words in it and hilarity into it.. 0:39 So we can do that right. 0:43 No problem. 0:45 Now one thing I want to show you is how do include more than one variable 0:46 into a formatted string. 0:49 Are you ready? 0:51 Let's dive back into work spaces. 0:52 We're going to launch a new workspace with a new starting point. 0:54 Now don't worry. 0:57 Your work from the previous day stage is saved and you can get back to it later. 0:58 Please click the Launch Workspaces button. 1:01 All right, I've added a new starter file for 1:04 you that has a new starting point or boilerplate that we're going to be using. 1:06 I've named the program TreeStory. 1:10 So let's open up TreeStory.java. 1:12 The first thing I wanna show you right off about here is a multiline comment. 1:16 When you wanna group a section of comments together, you can work a whole block of 1:21 comments by surrounding them using a forward slash and then an asterisk. 1:24 And then you end that, when you're done with your comments, with an asterisk and 1:29 then a forward slash. 1:32 I have left us some notes here about 1:33 parts of speech in case it's been awhile since we thought about that sort of stuff. 1:36 So like I said, because there's going to be multiple blanks to fill and 1:39 we're going to need to learn how to insert multiple variables into our format string. 1:42 Well this turns out to be super easy. 1:46 So first we need to gather a couple of variables like we did before. 1:49 Let's grab their name. 1:52 So again, to do that 1:53 they say we declare a variable string name equals console.readline. 1:55 And then we put in what we're looking for enter your name. 2:00 Put a couple spaces. 2:06 And then of course the semi-colon, 2:08 then we need to do the same thing for the adjectives. 2:11 So we say string, it's a string adjective. 2:13 You know what, why don't you pause the video or air and 2:15 see if you can finish the rest of the line and then un-pause it and check and 2:19 see if what you got was the same as what I did. 2:22 Sound good? 2:24 Okay. So I'm going to write equals consul.read 2:26 line, enter an adjective. 2:30 And I get only a couple spaces. 2:34 The print off method on console takes multiple parameters. 2:39 I had stated before the arguments are separated by commas. 2:42 So let's take a look at what this might look like. 2:44 So we'll start with our usual console.printf and 2:47 we'll pass it the string, percent S. 2:53 Is very percent S. 2:56 And so the first percent S there is gonna be our name. 3:01 And the second one that we want to do is the adjective. 3:03 So again it will be name is very adjective. 3:07 All right let's give that a go. 3:11 One trick I wanted to show you while we’re here, 3:13 you might not know while it’s on the command line yet. 3:14 But if you do each command, and then follow it with double ampersands, 3:16 the run in order until one fails. 3:20 So since we keep typing the In the same thing ,let's just type that 3:23 one some one line. 3:25 Let's try it. 3:26 So when I say clear and we're gonna do the compiler's which is Java C in the name 3:29 of the program which is Tree Story that Java their way to do another and then do. 3:35 Okay, so again, so what's going to happen is it's going to run clear and 3:45 if that works. 3:49 It's going to run the compiler and if that works then it will run the program. 3:50 One thing to do, always gotta remember to save. 3:54 So it's saved. 3:58 Here we go..When I press enter there we go, it cleared the screen it compiled and 3:59 now it's running the program so we're going to enter your name and say Dave. 4:04 Smart. 4:08 Dave is very smart. 4:10 Now having learned how to add multiple variables to a format string, 4:13 I'm pretty confident that you have all the skills 4:17 that you'll need to complete our tree story prototype. 4:20 Now before we kick off and start building out our own story templates. 4:23 I thought the swing over to an exercise. 4:27 And after you ace that one, 4:29 let's talk quickly about what to do when you encounter errors. 4:31
You need to sign up for Treehouse in order to download course files.Sign up