Welcome! In today's lesson, we're delving into String Formatting in Java, an essential feature for presenting data in a neat manner. We'll be exploring the intricate details of format strings, as well as methods such as printf
and String.format
. Ready? Let's get started!
Our journey begins with understanding the concept of String formatting. This concept reshapes the way we view a string. In Java, it's somewhat akin to enhancing the appearance of existing data. Specifically, format strings are tools we use to 'dress up' our data. These strings employ a delimiter %
, along with flags, width, and precision specifications to format the data. A simple example can illustrate this concept quite well:
Java1String name = "Tom"; 2String greeting = String.format("Hello, %s!", name); 3System.out.println(greeting); // Prints: Hello, Tom!
In this instance, we used %s
- a placeholder for a string - that gets replaced by the string Tom
.
The other available options include:
%d
- integer number%f
- float number
Let's explore how width
and precision
can further refine the display of our string. Width
allows us to define a minimum number of characters, while precision sets the limit on the number of decimal digits or characters in a string:
Java1int number = 123; 2double percentage = 90.32167; 3 4// `%5d` specifies a minimum width of 5 characters for an integer, adding extra whitespaces to the beginning 5// `%-5d` is the same as `%5d`, but whitespaces are now added to the end of the number 6// `%.2f` limits the output to 2 decimal digits for a float number 7String formatted = String.format("Number: %5d, Percentage: %.2f", number, percentage); 8System.out.println(formatted); // Prints: Number: 123, Percentage: 90.32 9String formattedRight = String.format("Number: %-5d, Percentage: %.2f", number, percentage); 10System.out.println(formattedRight); // Prints: Number: 123 , Percentage: 90.32
In this case, additional spaces for number
are padded to print 5 characters (even though the number has just 3 digits), and percentage
is truncated to two decimal places. When we use formattedRight
and %-5d
, extra whitespaces are added to the right instead.
To enhance the data display, we can adjust alignment and padding, filling extra spaces with specific characters. In this example, we fill the additional spaces with 0
:
Java1int number = 10; 2// `%05d` specifies the number will be returned with 5 characters in it, extra digits will be filled with 0 3String formatted = String.format("Number [%05d]", number); 4System.out.println(formatted); // Prints: Number [00010]
This is particularly useful when numbers need to align to the right and have the same number of digits.
Java provides printf
, an alternative to String.format
, for string formatting. printf
is similar to String.format
, but it prints directly to the console. Here's how it works:
Java1int number = 123; 2System.out.printf("Number: [%05d]", number); // Prints: Number: [00123]
To wrap up, let's consider a practical application of string formatting. Suppose we need to print a report on students' grades in a table-like structure:
Java1String header = String.format("| %-10s | %-5s | %-5s | %-5s |", "Name", "Math", "Sci", "Art"); 2String johnData = String.format("| %-10s | %-5d | %-5d | %-5d |", "John", 78, 82, 94); 3String annaData = String.format("| %-10s | %-5d | %-5d | %-5d |", "Anna", 92, 87, 88); 4 5System.out.println(header); // Prints: | Name | Math | Sci | Art | 6System.out.println(johnData); // Prints: | John | 78 | 82 | 94 | 7System.out.println(annaData); // Prints: | Anna | 92 | 87 | 88 |
Such formatting substantially improves data readability.
This example is pretty advanced, so don't hesitate to ask me to clarify it if you need any help!
Great job! We've covered String formatting in Java. Now, it's time to put your newly acquired skills to the test. Remember, practice is key to solidifying concepts and automating your coding skills. Let's move on to the exercises. Happy coding!