File size: 3,880 Bytes
33852e5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | 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.
|