Tan115's picture
Add files using upload-large-folder tool
33852e5 verified
Raw
History Blame Contribute Delete
3.88 kB
1
00:00:00,000 --> 00:00:01,000
Hello, Tim.
2
00:00:01,000 --> 00:00:07,000
In this lesson, I'd like to show you how you can format your string a bit later in this training course.
3
00:00:07,000 --> 00:00:13,000
We'll write with you your own web application, and you will have standard labels to ensure amazing
4
00:00:13,000 --> 00:00:16,000
user experience in your web application.
5
00:00:16,000 --> 00:00:21,000
For example, you will have personalized welcome message which will contain user first name and greetings.
6
00:00:22,000 --> 00:00:25,000
Greetings will be different based on the current time of the day.
7
00:00:26,000 --> 00:00:28,000
Your template string can look like this.
8
00:00:29,000 --> 00:00:33,000
Hello dear, some name and good part of the day.
9
00:00:33,000 --> 00:00:39,000
During the string formatting, you will be able to insert different strings instead of percent sign
10
00:00:39,000 --> 00:00:39,000
s.
11
00:00:40,000 --> 00:00:43,000
Here you have morning, afternoon and evening strings.
12
00:00:43,000 --> 00:00:49,000
To format string, you need to call string format method where the first argument will be string you
13
00:00:49,000 --> 00:00:56,000
want to format, and after that you will pass arguments referenced by the format specifiers like these
14
00:00:56,000 --> 00:00:57,000
signs.
15
00:00:58,000 --> 00:01:05,000
In our template we have two format specifiers in case you will pass more than two arguments after the
16
00:01:05,000 --> 00:01:10,000
template, they will be ignored in case you will pass not enough arguments.
17
00:01:10,000 --> 00:01:14,000
Exception will be thrown by JVM during the runtime format.
18
00:01:14,000 --> 00:01:20,000
Method returns the reference to the new string object and we can print it to console.
19
00:01:21,000 --> 00:01:22,000
Hello dear Andre.
20
00:01:22,000 --> 00:01:23,000
Good morning.
21
00:01:24,000 --> 00:01:28,000
Also you can print formatted string directly to console.
22
00:01:28,000 --> 00:01:31,000
You can use printf method for such purpose.
23
00:01:32,000 --> 00:01:36,000
Here you see different format specifier percent sign.
24
00:01:36,000 --> 00:01:36,000
D.
25
00:01:37,000 --> 00:01:40,000
This specifier is used to put any digit instead of it.
26
00:01:41,000 --> 00:01:44,000
And here you can see that I am passing integer.
27
00:01:44,000 --> 00:01:47,000
In console you can see the result which I get.
28
00:01:48,000 --> 00:01:52,000
There are different specifiers and flags and I prepared examples for you.
29
00:01:53,000 --> 00:01:57,000
You can find source code in separate file formatted demo.
30
00:01:58,000 --> 00:02:03,000
I will share a link with you to this file in the lesson resources or in video description.
31
00:02:04,000 --> 00:02:09,000
Here you will be able to find link to the official Oracle documentation where all specifiers and flags
32
00:02:09,000 --> 00:02:10,000
are listed.
33
00:02:11,000 --> 00:02:14,000
Let me run this program to walk you through the program output.
34
00:02:15,000 --> 00:02:22,000
The examples describe different ways to format numbers, justification, hexadecimal numbers, formatting,
35
00:02:22,000 --> 00:02:30,000
precision formatting, etc. I don't think that it is necessary to pay attention to each example here
36
00:02:30,000 --> 00:02:35,000
in this video, because from the syntax point of view, they are all similar.
37
00:02:35,000 --> 00:02:39,000
The only thing you need to learn is different specifiers and when to use them.
38
00:02:40,000 --> 00:02:45,000
During your homework, I would ask you to run these examples on your computer and to ask me questions
39
00:02:45,000 --> 00:02:46,000
in case you will have any.
40
00:02:47,000 --> 00:02:48,000
Thanks a lot for your attention.
41
00:02:48,000 --> 00:02:50,000
See you in the next lesson.