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 "listen" to the user by accepting input from the keyboard. We will then use this input to dynamically change our output to the screen.
Resources
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
So far,
we've been sending messages to our users,
0:00
but it feels a little one-sided,
doesn't it?
0:03
Let's make our program
listen to what the user types in, too.
0:06
Up until now, we've only done output,
showing text on the screen.
0:10
Now we'll add input, which means
getting information from the user,
0:14
specifically from the keyboard.
0:17
Let's go back to our workspace
and learn how to take input.
0:19
Right now,
our variable firstName is fixed.
0:24
It's hard-coded
with a specific value.
0:27
Let's make it more flexible
by asking the user for their name instead.
0:29
Remember how I said that objects Console
have methods that let them take actions?
0:33
So far we've used only one method, printf,
which prints text to the screen.
0:38
But the same Console object
has another method called readLine.
0:44
Just printf prints text, readLine
0:48
will print a prompt
and then let the user type something.
0:51
It captures what the user types
and gives it back to us.
0:54
Let's try it.
0:58
First, let's remove the fixed
string assignment for our first name.
0:59
Then type console.readLine.
1:03
Notice that's camel case there.
1:06
This is a method and we're calling it,
1:09
so we put in our parentheses
and end with our semicolon.
1:11
Much printf, inside the parentheses,
1:15
we can provide the string
that we want printed to the user.
1:18
What is your name?
1:23
Notice
the two spaces after the question mark.
1:27
This just makes the cursor
appear nicely spaced after the prompt.
1:30
So here's what's happening.
The readLine method returns
1:34
a value, in this case
a string, that the user types.
1:37
Methods can perform actions and also
return information when they finish.
1:41
We use the value returned by readLine
to set the variable firstName.
1:46
Then just like before, we use firstName
1:52
to print sentences with the user's input.
1:54
Let's see this in action by compiling
1:58
and running our file.
1:59
Okay, we'll see the prompt,
What is your name?
2:07
And notice the cursor blinking
right after the two spaces.
2:10
When we type a name, Dustin,
and press enter, the program
2:14
will use that name wherever our firstName
variable appears, just like before.
2:18
Your program
2:24
now changes dynamically based on the value
the user provides.
2:25
Nice!
2:28
We've learned how to get input
from the keyboard
2:31
in a command line program
and print formatted output.
2:33
This combination of input and output
is called I.O., short for input-output.
2:37
In fact,
if you look at the top of your code,
2:42
you'll see this line, import
java.io.console.
2:44
This means we're using the console type
from the java.io package.
2:49
Java packages
group related tools and code together.
2:54
We'll learn more about packages later.
2:57
But I wanted to point out
that the term I.O.
3:00
you just learned
is right there in the package name.
3:02
Congratulations!
3:05
You already know how to send output
to the screen,
3:06
receive input from the user, store data
in your variables,
3:09
and use that data in your program
dynamically.
3:12
These are fundamental skills
that you'll use
3:16
in almost every interactive program
that you write.
3:18
Since these concepts are so important,
let's finish this section
3:21
with an exercise to practice them.
3:24
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