Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
This video will present the solution to workbook 2.5 from VES Code.
Make sure to launch the exact folder that contains your Java file in this case, Workbook 2.5 and Task
one tells us to enter in some information.
So first I will say system dot out dot print line asking the user for their first name.
What is your first name?
And once we do that, we're going to say scan next line.
So once this line runs, it's going to wait for the user to enter in a string value.
And after scan, the next line picks up the string value that the user submits we need to store.
The resulting string inside of a variable called.
In this case, first name.
We'll do the same thing for the last name.
What is your last name?
Once the runtime reaches here, we're going to pick up the string value that they submit and we're going
to store that in a variable called last name.
Then we ask them how old they are.
So we'll say, System.out.println.
How old are you?
Scandal.
Next int is going to wait for the user to submit an integer.
And once we pick up the integer that they submit, we're going to store it in a variable called H.
We'll do the same thing for the username and for the username.
We need to ask them to create one.
So we'll say system, not that print line.
Please create a username.
And then we will repeat this process by saying string username is equal to whatever string value gets
returned from skin.
Next line.
Okay, I'm going to stop at the username for now.
Let me just run the code by saying Java C compiling the code inside of sign up dot Java and then running
the compiled code.
So when the code reaches this line, you'd ask them for their first name scanned.
Next line waits for us to enter in a string.
So I'm going to enter in the string, Ryan.
That string is going to get picked up and stored in a variable called first name.
And after asking the user how old they are, next int is going to wait for them to enter an integer,
which they are.
And what just happened.
So after scanned out next and grabbed the integer and stored it inside the variable age.
The next step would have been for the console to print.
Please create a username, which it did, but then scanned out.
Next line.
Should have waited for the user to enter in a username, but it got completely skipped.
Why did that happen?
I warned you about the next line trap in that next line gets completely skipped when placed after a
next int, a next double or a next log and the solution is to place an extra throwaway next line before
the real next line.
But you might be wondering why does this happen?
I do explain why this happens eventually once you reach the delimiter lesson, but for now the simple
solution is to add a throwaway next line and extra next line.
Right before the one that matters.
That way.
The next line that we don't care about gets skipped.
But the next one will, as always, wait for the user to enter a string, which were then going to store
inside of a string variable called username.
So let's see all of this in action.
I'm going to input a name.
Ryan Adams will make them 35.
And beautiful, while the throwaway next line that we don't care about gets completely skipped, it
gets wasted.
The next line that follows wait for the user to enter a string.
I'm going to enter the string are slim 087.
That string is going to get picked up and stored in a variable called username.
All right.
Now feel free to pause the video and complete task one on your own.
That's all for task number one.
Task number two is to print their information.
So the first thing I'm going to say is system, dot, dot, print line.
And instead of just your information, what I'll do is I'll say, here is the information you entered
again, that's up to you.
Not too important what string you print.
But what I want to do is create a line of space between Thank you for joining Java Gram and here is
the information you entered.
And what I can do is use the escape character backslash, which basically tells Java, Hey, look out
for the next character.
It's special.
And by saying n.
Java is going to know to insert a new line right over here.
So let's try it out.
Java Sea.
Sign up to Java running the compiled code.
What is your first name?
Ryan Adams will do this quickly.
35 are slim 087.
Ottawa, Canada.
Notice the extra line of space that was inserted between.
Thank you for joining Java Gram and here is the information that you entered.
Looking back at the final output, a tab of space is inserted before printing each piece of information.
So what we could do is something like this system dot out, dot print line.
On a new line we will print first name.
Um, whatever the variable value ends up being.
First name.
And in order to create a tub of space, I can simply press space four times.
But that's really ugly.
What I can do is use the escape character once again telling Java, Hey, the character that follows
is special, and by putting a T, Java knows, okay, I need to insert a tab of space right here.
I'll give you some time to pause the video so that you can do this for the remaining variables.
Before we start, you should always close scanner.
Otherwise you're going to get a memory leak.
It's always, always good to close scanner when you're done with it.
Otherwise you're wasting resources.
This becomes especially more prevalent when you're building a much bigger application.
For example, if you're ever using scanner in a spring boot web application, you better make sure to
close it.
Otherwise you're going to get a very, very big leak.
All right, let's compile our code and run it.
First name, Ryan.
Last name, Adams.
How old are you?
Will say he's 35.
Are slim.
Zero eight or nine?
Eight six.
Ottawa, Canada.
And this looks great.
Before we wrap up, If you want, feel free to visualize the first part of our code.
Next line picks up the name.
And the string it picks up gets stored in a variable.
Next line picks up the last name and the string it picks up gets stored in a variable next and picks
up the age, which we then store in a variable.
And when next line comes after your next int, next double, next longer and next it gets wasted, it
gets skipped.
But the next line that follows will work business as usual.
You will understand why this trap happens once you reach the lesson on.
Mm.