Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,150 --> 00:00:03,840
Before we start, did you try solving the workbook yourself?
2
00:00:03,870 --> 00:00:09,630
If not, please click the link in the resources folder and follow the instructions for this workbook.
3
00:00:10,780 --> 00:00:14,890
This video will present the solution to workbook 2.5 from VES Code.
4
00:00:14,980 --> 00:00:21,970
Make sure to launch the exact folder that contains your Java file in this case, Workbook 2.5 and Task
5
00:00:21,970 --> 00:00:25,030
one tells us to enter in some information.
6
00:00:25,030 --> 00:00:32,520
So first I will say system dot out dot print line asking the user for their first name.
7
00:00:32,530 --> 00:00:35,110
What is your first name?
8
00:00:37,220 --> 00:00:40,540
And once we do that, we're going to say scan next line.
9
00:00:40,550 --> 00:00:46,580
So once this line runs, it's going to wait for the user to enter in a string value.
10
00:00:46,820 --> 00:00:52,820
And after scan, the next line picks up the string value that the user submits we need to store.
11
00:00:54,150 --> 00:00:57,810
The resulting string inside of a variable called.
12
00:00:58,110 --> 00:00:59,790
In this case, first name.
13
00:01:00,960 --> 00:01:02,850
We'll do the same thing for the last name.
14
00:01:03,540 --> 00:01:05,129
What is your last name?
15
00:01:05,129 --> 00:01:10,800
Once the runtime reaches here, we're going to pick up the string value that they submit and we're going
16
00:01:10,800 --> 00:01:13,590
to store that in a variable called last name.
17
00:01:14,990 --> 00:01:17,050
Then we ask them how old they are.
18
00:01:17,060 --> 00:01:19,850
So we'll say, System.out.println.
19
00:01:20,810 --> 00:01:22,250
How old are you?
20
00:01:24,880 --> 00:01:25,610
Scandal.
21
00:01:25,780 --> 00:01:29,980
Next int is going to wait for the user to submit an integer.
22
00:01:30,340 --> 00:01:37,600
And once we pick up the integer that they submit, we're going to store it in a variable called H.
23
00:01:41,020 --> 00:01:45,070
We'll do the same thing for the username and for the username.
24
00:01:45,070 --> 00:01:48,640
We need to ask them to create one.
25
00:01:48,640 --> 00:01:50,680
So we'll say system, not that print line.
26
00:01:51,070 --> 00:01:54,400
Please create a username.
27
00:01:57,100 --> 00:02:04,870
And then we will repeat this process by saying string username is equal to whatever string value gets
28
00:02:04,870 --> 00:02:06,640
returned from skin.
29
00:02:07,090 --> 00:02:08,169
Next line.
30
00:02:11,570 --> 00:02:13,880
Okay, I'm going to stop at the username for now.
31
00:02:13,910 --> 00:02:21,260
Let me just run the code by saying Java C compiling the code inside of sign up dot Java and then running
32
00:02:21,260 --> 00:02:22,610
the compiled code.
33
00:02:24,030 --> 00:02:28,680
So when the code reaches this line, you'd ask them for their first name scanned.
34
00:02:28,800 --> 00:02:32,100
Next line waits for us to enter in a string.
35
00:02:32,100 --> 00:02:35,280
So I'm going to enter in the string, Ryan.
36
00:02:37,100 --> 00:02:41,780
That string is going to get picked up and stored in a variable called first name.
37
00:02:47,950 --> 00:02:53,350
And after asking the user how old they are, next int is going to wait for them to enter an integer,
38
00:02:53,890 --> 00:02:55,000
which they are.
39
00:02:56,800 --> 00:02:58,460
And what just happened.
40
00:02:58,480 --> 00:03:04,540
So after scanned out next and grabbed the integer and stored it inside the variable age.
41
00:03:04,570 --> 00:03:07,030
The next step would have been for the console to print.
42
00:03:07,030 --> 00:03:10,630
Please create a username, which it did, but then scanned out.
43
00:03:10,630 --> 00:03:11,200
Next line.
44
00:03:11,200 --> 00:03:16,240
Should have waited for the user to enter in a username, but it got completely skipped.
45
00:03:16,270 --> 00:03:17,830
Why did that happen?
46
00:03:18,130 --> 00:03:24,760
I warned you about the next line trap in that next line gets completely skipped when placed after a
47
00:03:24,760 --> 00:03:33,070
next int, a next double or a next log and the solution is to place an extra throwaway next line before
48
00:03:33,070 --> 00:03:34,990
the real next line.
49
00:03:35,080 --> 00:03:37,840
But you might be wondering why does this happen?
50
00:03:37,870 --> 00:03:44,830
I do explain why this happens eventually once you reach the delimiter lesson, but for now the simple
51
00:03:44,830 --> 00:03:49,900
solution is to add a throwaway next line and extra next line.
52
00:03:51,100 --> 00:03:53,410
Right before the one that matters.
53
00:03:55,800 --> 00:03:56,370
That way.
54
00:03:56,370 --> 00:03:59,400
The next line that we don't care about gets skipped.
55
00:03:59,400 --> 00:04:06,330
But the next one will, as always, wait for the user to enter a string, which were then going to store
56
00:04:06,330 --> 00:04:09,330
inside of a string variable called username.
57
00:04:09,480 --> 00:04:11,640
So let's see all of this in action.
58
00:04:15,600 --> 00:04:17,820
I'm going to input a name.
59
00:04:18,120 --> 00:04:22,920
Ryan Adams will make them 35.
60
00:04:25,920 --> 00:04:32,250
And beautiful, while the throwaway next line that we don't care about gets completely skipped, it
61
00:04:32,250 --> 00:04:33,360
gets wasted.
62
00:04:33,390 --> 00:04:37,970
The next line that follows wait for the user to enter a string.
63
00:04:37,980 --> 00:04:41,250
I'm going to enter the string are slim 087.
64
00:04:41,250 --> 00:04:45,960
That string is going to get picked up and stored in a variable called username.
65
00:04:48,130 --> 00:04:49,270
All right.
66
00:04:49,960 --> 00:04:54,310
Now feel free to pause the video and complete task one on your own.
67
00:04:59,890 --> 00:05:01,600
That's all for task number one.
68
00:05:01,600 --> 00:05:04,270
Task number two is to print their information.
69
00:05:04,270 --> 00:05:09,520
So the first thing I'm going to say is system, dot, dot, print line.
70
00:05:09,520 --> 00:05:16,510
And instead of just your information, what I'll do is I'll say, here is the information you entered
71
00:05:16,720 --> 00:05:18,420
again, that's up to you.
72
00:05:18,430 --> 00:05:21,100
Not too important what string you print.
73
00:05:22,110 --> 00:05:30,360
But what I want to do is create a line of space between Thank you for joining Java Gram and here is
74
00:05:30,360 --> 00:05:31,680
the information you entered.
75
00:05:31,680 --> 00:05:39,210
And what I can do is use the escape character backslash, which basically tells Java, Hey, look out
76
00:05:39,210 --> 00:05:40,470
for the next character.
77
00:05:40,470 --> 00:05:41,490
It's special.
78
00:05:41,640 --> 00:05:43,500
And by saying n.
79
00:05:44,490 --> 00:05:49,320
Java is going to know to insert a new line right over here.
80
00:05:49,710 --> 00:05:51,180
So let's try it out.
81
00:05:51,210 --> 00:05:51,860
Java Sea.
82
00:05:51,870 --> 00:05:54,930
Sign up to Java running the compiled code.
83
00:05:54,960 --> 00:05:56,280
What is your first name?
84
00:05:57,070 --> 00:06:00,250
Ryan Adams will do this quickly.
85
00:06:00,250 --> 00:06:03,580
35 are slim 087.
86
00:06:06,300 --> 00:06:08,280
Ottawa, Canada.
87
00:06:10,380 --> 00:06:14,860
Notice the extra line of space that was inserted between.
88
00:06:14,880 --> 00:06:20,130
Thank you for joining Java Gram and here is the information that you entered.
89
00:06:21,220 --> 00:06:27,940
Looking back at the final output, a tab of space is inserted before printing each piece of information.
90
00:06:28,090 --> 00:06:34,420
So what we could do is something like this system dot out, dot print line.
91
00:06:34,510 --> 00:06:37,900
On a new line we will print first name.
92
00:06:39,030 --> 00:06:43,070
Um, whatever the variable value ends up being.
93
00:06:43,080 --> 00:06:44,010
First name.
94
00:06:44,840 --> 00:06:50,300
And in order to create a tub of space, I can simply press space four times.
95
00:06:50,300 --> 00:06:51,720
But that's really ugly.
96
00:06:51,740 --> 00:06:58,100
What I can do is use the escape character once again telling Java, Hey, the character that follows
97
00:06:58,100 --> 00:07:05,720
is special, and by putting a T, Java knows, okay, I need to insert a tab of space right here.
98
00:07:05,750 --> 00:07:10,010
I'll give you some time to pause the video so that you can do this for the remaining variables.
99
00:07:15,630 --> 00:07:18,560
Before we start, you should always close scanner.
100
00:07:18,570 --> 00:07:21,240
Otherwise you're going to get a memory leak.
101
00:07:21,630 --> 00:07:24,840
It's always, always good to close scanner when you're done with it.
102
00:07:24,840 --> 00:07:27,250
Otherwise you're wasting resources.
103
00:07:27,270 --> 00:07:32,120
This becomes especially more prevalent when you're building a much bigger application.
104
00:07:32,130 --> 00:07:38,220
For example, if you're ever using scanner in a spring boot web application, you better make sure to
105
00:07:38,220 --> 00:07:38,990
close it.
106
00:07:39,000 --> 00:07:42,570
Otherwise you're going to get a very, very big leak.
107
00:07:42,900 --> 00:07:47,520
All right, let's compile our code and run it.
108
00:07:47,940 --> 00:07:49,800
First name, Ryan.
109
00:07:50,070 --> 00:07:51,930
Last name, Adams.
110
00:07:51,930 --> 00:07:52,860
How old are you?
111
00:07:52,860 --> 00:07:55,350
Will say he's 35.
112
00:07:55,680 --> 00:07:57,030
Are slim.
113
00:07:57,030 --> 00:07:58,440
Zero eight or nine?
114
00:07:58,440 --> 00:07:59,370
Eight six.
115
00:08:00,060 --> 00:08:02,670
Ottawa, Canada.
116
00:08:04,770 --> 00:08:06,870
And this looks great.
117
00:08:08,030 --> 00:08:12,780
Before we wrap up, If you want, feel free to visualize the first part of our code.
118
00:08:12,800 --> 00:08:14,630
Next line picks up the name.
119
00:08:17,020 --> 00:08:19,600
And the string it picks up gets stored in a variable.
120
00:08:19,630 --> 00:08:25,270
Next line picks up the last name and the string it picks up gets stored in a variable next and picks
121
00:08:25,270 --> 00:08:28,090
up the age, which we then store in a variable.
122
00:08:28,090 --> 00:08:35,049
And when next line comes after your next int, next double, next longer and next it gets wasted, it
123
00:08:35,049 --> 00:08:36,070
gets skipped.
124
00:08:36,070 --> 00:08:40,419
But the next line that follows will work business as usual.
125
00:08:41,980 --> 00:08:46,330
You will understand why this trap happens once you reach the lesson on.
126
00:08:46,390 --> 00:08:47,320
Mm.
11249
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.