In part one, you learn to use scanner methods next line, which works for the user to enter a string
value and next and to watch waits for the user to enter an integer next double, which waits for the
user to enter a double value and using these scanner methods.
We ask the user a lot of questions and the user responded to each one.
So in this lesson, our application is going to respond back to the user.
All right, after the user answers every question, it would be nice to respond with a thank you for
answering all five questions.
Now, should we hard code the number five directly inside the print statement?
I don't think so.
Imagine this was a bigger application with dozens of questions or I dare say hundreds of questions.
Are you going to keep counting the questions every time you add a new one?
I'd be more comfortable having Java count the number of questions for me.
So what I'm going to do is make an entire variable counter and first it's going to store the value of
zero.
Then we can use the add one operator plus plus that we learned about and that's going to increase the
counter value by one every time we ask a question.
And so in the survey costs, we're going to make it into variable counter at the very top end counter
and set it equal to zero.
And now we're going to increase the counter value by one after each question.
Counter plus plus here.
Counter plus plus.
And do that two more times.
One last time.
Once the code gets to hear, the counter value should be five.
So at this point, we have the users values.
We counted the questions.
It's time to respond.
We're going to print a message that refers to the user by name, so system, dot, dot, print line.
We're going to say thank you.
Break the string and put the user's name.
And then we'll reconnect the string and say thank you, some name for answering all.
Break the string and put the counter value.
And then right questions at the end, so this should say thank you some person, for answering all X
amount of questions.
Let's recompile the code.
And runit.
What is your name?
Sam will say a dollar fifty four dollars and fifty cents.
Five and seven.
And check it out, the application responds.
It refers to the user by name and thanks them for answering all five questions.
Now, if you look at your terminal, the response and the last input are a bit squished, it looks kind
of awkward.
There there's a special symbol, again, that creates a new line of space, the backslash is an escape
character.
It tells Jarvis Stop the next character special and the character that follows is and where and again
creates a new line.
So in your code, we're going to add again at the beginning of our response.
All right, let's run our code.
And check it out, it does create a new line of space for us, Jarvis sees the slash in Java, knows
Slash is an escape character, sits there and creates a new line of space.
And after the new line, it prints the rest of the string as normal.
All right, we have that figured out now the app is going to tell the user how much more money they
spend on fast food over coffee.
So we'll say system dot out, dot print line.
Your fast food expenses are.
Break the string and here we'll need to divide the food price by the coffee price.
And we're going to blend the result of this operation as part of this long string, let's reconnect
another string that says times more than your coffee expenses.
When I recompile my code.
Your fast food expenses are three times more than your coffee expenses.
Notice that when decimals are involved in the operation, you get back a decimal.
All right, now we're going to tell the user how much they spend on coffee and food in a week, so we
need to multiply the amount and price for food and coffee.
System dart out the front line.
Weekly.
You spend.
In dollars.
Break the string and we're going to blend in the result of multiplying coffee amount by coffee price.
Then we'll reconnect the string to say on coffee.
We'll do the exact same thing for food system out front line.
Weekly you spend in dollars will blend in the result of multiplying the food amount by the food price.
And will conclude the string to say on food.
All right, the recompiling the code and running it, I'm going to fast forward.
And this looks good.
Once again, the result is a decimal because the price values are of type double.
Have another look at the final output.
You'll notice that an empty line precedes each question, so we're going to add a slash and before the
start of each question.
And here.
All right, rerun your app.
And notice now that the output looks a lot cleaner.
Congratulations, you built your first dynamic application in Java, the application starts by asking
these are questions and using scanner, it picks up each answer and stores it in a variable.
And now that we have every value stored in a variable, the application can respond.
As a final result, you built a fully interactive application where the user communicates with the app
and the app communicates back.
And that's all for your first dynamic up there is much more to come in this course, and I hope you're
excited.