Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Basics!
You have completed Java Basics!
Preview
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.
Additional Info
- The official Mad Libs site. Download the app!
- A Stack Exchange article about logic on the command line.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Now that we
0:05
know how to take input
and output it in formatted text,
0:05
we're ready to start building what
we're going to be working on throughout
0:08
the rest of this course,
a Mad Libs-style program.
0:12
By the end of this section,
you'll have a working prototype.
0:15
First, let's quickly review
how Mad Libs work in real life.
0:19
Two people get together,
and one has a book.
0:23
Each page has a story with blank spaces
labeled with word types like
0:26
adjective, noun, verb, and so on.
0:30
The narrator with the book asks the other
person to provide words for each blank.
0:34
Then the story is read aloud
with those new words
0:39
filled in, often
creating hilarious results.
0:42
We'll do the same thing in our program.
0:45
So we can do that, right? No problem.
0:47
One thing I want to show you
0:50
is how to include more than one variable
in a formatted string.
0:51
Now we're going to be opening a new
workspace with a fresh starting point.
0:55
And don't worry,
0:59
your previous work is saved
and you can come back to it at any time.
1:00
So please click
the Launch Workspace button below now.
1:03
Okie dokie in this new workspace
1:09
I've added a new starter file
for you called TreeStory.java
1:11
with some starter or boilerplate
1:15
code inside. Let's open it.
1:17
The first thing I want to show
you is a multi-line comment.
1:20
When you want to write a comment
that covers several lines,
1:23
you can surround it with a forward slash
asterisk, or star, at the start
1:26
and a star forward slash at the end.
1:31
I've left some notes here
about parts of speech, just in case it's
1:35
been a while since you thought
about nouns, adjectives, verbs, and so on.
1:38
It's definitely something
I always need to double-check.
1:42
Because our story
will have multiple blanks to fill,
1:45
we need to learn how to insert
multiple variables into our format string.
1:48
It's actually very easy.
1:52
So first, we need to ask the user
for several inputs, just like before.
1:54
For example, to get
their name, let's write string
1:59
name equals
2:03
console.readLine,
2:07
and we'll say enter your name
2:11
and leave a space at the end again.
2:13
Perfect! Don't forget your semicolon.
2:18
For the adjective
2:24
we do the same. String adjective
and you know what? ...
2:25
Why don't you pause the video here and try
writing the rest of the line yourself?
2:31
Then unpause to check
if your code matches mine.
2:35
Sound good? Cool.
2:38
Go ahead and pause me.
2:39
How'd you do?
2:43
If you got stuck, don't worry.
2:44
Here's how we can do it.
2:46
We can say equals console.readLine.
2:47
Enter an adjective.
2:55
Awesome.
3:00
All right, so now we create
a format string just like before,
3:02
but we can add multiple placeholders
and multiple arguments.
3:05
Arguments are what we call the values
we pass into these parameters.
3:09
So we can say console.printf %s
3:14
is very %s.
3:21
Then we'll give name,
3:26
another comma and adjective.
3:28
The first placeholder will be replaced
by the name variable
3:32
and the second by the adjective. Running
this code might output something like
3:35
Travis is very hungry,
based on the inputs the user gave.
3:40
Make sure your file is saved.
3:44
Oh, here's a handy command line trick.
3:46
If you type commands
separated by double ampersands,
3:48
they will run one after the other.
3:52
Since we're constantly
3:54
rerunning three different commands,
let's consolidate it down to one.
3:55
We can say clear, double ampersand,
3:59
and we'll compile TreeStory.java,
4:04
and we'll run TreeStory.
4:09
When I press enter
after typing that, the screen
4:14
clears, the program compiles,
and then runs our code.
4:16
You can provide a name, say Rachel,
followed by an adjective.
4:21
I'll say smart, and see the output.
4:25
Rachel is very smart. Awesome.
4:28
Now that you
4:32
know how to add multiple variables
to a format string,
4:32
I'm confident you're ready to start
to build our tree story prototype.
4:35
Before we start creating our own story
templates, let's do a quick exercise
4:39
to practice.
4:43
After that, we'll talk about what
to do when you encounter errors.
4:44
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up