Hello, Tim.
In this lesson, I'd like to show you how you can format your string a bit later in this training course.
We'll write with you your own web application, and you will have standard labels to ensure amazing
user experience in your web application.
For example, you will have personalized welcome message which will contain user first name and greetings.
Greetings will be different based on the current time of the day.
Your template string can look like this.
Hello dear, some name and good part of the day.
During the string formatting, you will be able to insert different strings instead of percent sign
s.
Here you have morning, afternoon and evening strings.
To format string, you need to call string format method where the first argument will be string you
want to format, and after that you will pass arguments referenced by the format specifiers like these
signs.
In our template we have two format specifiers in case you will pass more than two arguments after the
template, they will be ignored in case you will pass not enough arguments.
Exception will be thrown by JVM during the runtime format.
Method returns the reference to the new string object and we can print it to console.
Hello dear Andre.
Good morning.
Also you can print formatted string directly to console.
You can use printf method for such purpose.
Here you see different format specifier percent sign.
D.
This specifier is used to put any digit instead of it.
And here you can see that I am passing integer.
In console you can see the result which I get.
There are different specifiers and flags and I prepared examples for you.
You can find source code in separate file formatted demo.
I will share a link with you to this file in the lesson resources or in video description.
Here you will be able to find link to the official Oracle documentation where all specifiers and flags
are listed.
Let me run this program to walk you through the program output.
The examples describe different ways to format numbers, justification, hexadecimal numbers, formatting,
precision formatting, etc. I don't think that it is necessary to pay attention to each example here
in this video, because from the syntax point of view, they are all similar.
The only thing you need to learn is different specifiers and when to use them.
During your homework, I would ask you to run these examples on your computer and to ask me questions
in case you will have any.
Thanks a lot for your attention.
See you in the next lesson.