We can use scanner to make our applications interactive.
We've already built many applications using the five main data types.
Br view they are int to store whole numbers long for large real numbers, double for decimals, car
for characters and string for text.
But there is a small issue with the apps you've built so far, and that is that they've been static.
In other words, you've been storing predefined values.
Now in the real world, you want to make a dynamic application where values come from, the user.
Like here, for example, the application is asking the user questions and the user answers by submitting
values through the terminal.
And once the user answers every question, the application responds, this application is fully interactive
because the application and the user are communicating with each other.
So in this lesson, you're going to use scanner to get values from the user.
First thing I need you to do is create in your class, so inside the Section two project to make a new
file named Survey Java and inside the survey class, make sure it has the main method.
Scanner waits for the user to enter a value in the terminal.
In the survey app, you will ask the user questions such as How old are you?
And now Scanner needs to wait for the user to enter an integer.
We do that with scanned next minute.
And now the user must go to the terminal type a number and press enter.
Once the user presses enter, scanned next and picks up the integer and stores it in the variable.
We need to import scanner in order to use it.
Scanner comes from a package called Java Util.
So from Java, you tell we can import scanner.
And notice that we import at the very top.
So back to our code, we're going to import scanner, to the survey class import and from Java, you
tell we're going to import scanner.
Now we can access scanner inside the survey class.
Simple as that.
And so the next step is to set up scanners so that it can receive input from the user.
So we need to make a scanner variable, and that involves three steps.
The variable must be F-Type's scanner, the variable name I usually call it scan, but you can call
it anything and you're going to set the variable equal to a new scanner such that this scanner can get
input from the system system not in.
Scanner.
Skin.
Is equal to a new scanner instance.
That's going to receive input from the system system in.
And now we're going to use scanner to get a values from the user.
Scanner has different methods that we can leverage to get values from the user.
And next line is one of them.
Next line waits for the user to enter a string.
So we're going to start by giving the user a warm welcome.
So system died out the bright line.
Welcome, thank you for taking the survey.
Then we'll say system.
Dot out dot print line.
What is your name?
All right.
Let's compile the code and run this.
As you can see, this only prints two messages, then the app terminates.
We need to cull Skin Dot next line so that it can wait for the user to enter a stream value.
All right, let's try it out.
Compile your code.
Run it.
And check it out, it waits for me to enter a string.
I'm going to right, Sam.
And there you go.
What's happening is that Scandal Next line waits for the user to enter a string.
The user being me and the user went to the terminal and typed, Sam.
And once the user presses enter scan, the next line picks up the value.
But to be able to use this value in the future, we have to store it inside a variable.
So what I'm going to do is say string name is equal to scandal.
Next line.
So now, when the user enters their answer, scanned, the next line gets the string of value and stores
it in the variable name.
Next, stubble is another method we can call from Skinner.
This one waits for the user to enter a double value.
So now we're going to ask the user how much money they would spend on coffee.
So System died out die print line.
Money, do you spend?
On coffee.
And now, Renee, you stand up next, double to wait for the user to enter a double.
So we'll say double.
Coffee price.
Is equal to scan next double.
All right, we're compiling our code.
And running it.
What is your name, Sam?
How much money do you spend on coffee?
Let's say a dollar fifty.
So what happens is it picks up the value and stores it in a variable coffee price.
First, the programs they use or how much money they spend on coffee scanned next level, wait for the
user to enter a decimal.
It picks it up and stores it in a variable.
All right.
For more practice, we're going to ask the user how much money they spend on fast food system dart out
dart line.
How much money?
Do you spend on fast food?
And you're going to store the user's answer in a variable double food price is equal to scan Dot next
double.
What is your name, Sam?
How much money do you spend on coffee?
A dollar fifty?
How much money do you spend on fast food will say four dollars and fifty cents.
And once again scanned next, I will wait for the user to enter a decimal.
It picks it up and stores it in a variable.
Next is another method you can call from scanner.
And this one, as you could probably guess, it waits for the user to enter an integer.
So to start ominous, these are how many times a week they buy coffee and how many times a week they
buy fast food.
So system dot out dot print line.
How many times a week?
Do you buy coffee?
And I'm going to store the user's answers in a variable coffee amount.
And we're going to set that equal to scan next.
It.
Then we'll do the same thing here, system dot out dot print line.
How many times a week do you buy fast food?
And we'll store the user's answer in a variable food amount.
And we're going to get the user's answer by waiting for the next it's.
All right.
Recompile the code and run it.
What is your name?
Sam?
How much money you spend on coffee, a dollar?
Fifty four dollars and fifty cents?
How many times a week do you buy coffee?
Five.
Fast food seven.
All right.
This app is shaping up pretty well.
The first scandal, an accident, waits for the user to answer the key question.
Then it picks up the value in stories that in the amount, the second scanned on and waits for the user
to answer the fast food question.
Then it picks up the value and stores it in food amounts.
And now every variable stories is one of the users answers.
You might be wondering what if the user puts a wrong value?
And the second question, for example, let me just rerun my code.
When I put in the first answer and now here, what if I put a string, when next they will expect a
decimal?
Let's try up.
And Java throws an error and it crashes.
Next, we'll expect to decimal notice drink, and so if you enter a string next double doesn't know
what to do with it.
So the application crashes.
You should close the scanner when you're done with it.
Keeping scanner open is a memory leak.
When you're done with it, you can close it with scanned clubs.
Scan Duclos.
Now, if you scroll up notice that the warning is gone.
Let's recap in this lesson, you learn to use scanner to get values from the user.
Next line waits for the user to enter a string value.
And once the user presses enter scanned, our next line picks up the string and stores it in the variable.
Next double waits for the user to enter a double value, and once the user presses enter scan, the
next double picks up the double and stores it in the variable.
Next in, wait for the user to enter an into value.
And once the user presses enter scanned next and picks up the end and stores it in the variable.
Restore it every value that the user entered.
So what do we do now?
Stick around for part two.