Java Format Specifiers

In the previous tutorial, we demonstrated a lesson for Java initialization. If you have a lack of knowledge from the Java initialization, we recommend you first check out that lesson early. In this section, we are going to learn the Java format specifiers.

Java Format Specifiers

What exactly is this format specifier? We are very familiar with the variables and data types. If you need a revision, go into the backlinks and have a glance at the data types. Because in this section, we are going to work on these data types.

To specify data types in the printf() and string.format function using a “%” sign before the different formats in Java code is known as the Java format specifiers. Not so clear? Don’t worry, and we will make it easier. We all know that println() function output a printing line. So, Usually, to print something in Java code, we are very familiar with this println() function.

We can also print something on the output panel using printf() function (print format). But if we use a printf() role in java or C programming, we must declare the respective format specifiers for the exact position in the output panel. Let’s see an example.

package main;
public class First{
    public static void main( String args[] ) {
       String name = "infolinux";
       int year = 2018;
       System.out.printf("%s was established in %d.%n", name, year);
    } }
format specifiers - Java Format Specifier

Here from the example, observe the printing line. Similar to the println function, you can put the printf. Inside the procedure, you must set the position of your data and its type. Later outside the quota, you can serialize your variables based on the serial of the data types inside the allocation. “%s” determines the format specifier of the string “name.” On the other hand, “%d” specifies the format specifiers of the decimal (integer) variable.

Java Format Specifiers and its types

We can use this format specifiers in Java different conditions. But we don’t want to go too deep for now. Because here we are prioritizing the beginner. Therefore, let’s focus on the printf function and the uses of java format specifiers in printf function.

The following table includes all the necessary formats of the specifiers. You can easily distinguish them from the explanation section.

Format
Specifiers
Output
%aFloating-point Hex output
%bto specify the “true/false” if non-null/null
%cPrint a Unicode character
%dPrint any decimal values
%eFloating-point of the scientific notation of the decimal values
%fFloating-point of the decimal numbers
%gTop print floating-point of decimal numbers
 depending on the values and precision
%hHexagonal strings value from hashcode()
%nSpecifies the line separation
%oOctal number of the integer
%sAny string value
%tDate, time or calendar
%xHex strings from an integer.

Java sub-format Specifiers

Besides beautifying our output, we can not meet those requirements using the format specifiers only. Let’s say we need some space to organize the values or to arrange the list in order. Can you do that using only the format specifiers? It would help if you needed sub-specifiers.

Floating-point sub-specifiers formatting

If the output is related to floating-point formatting, Then we should use the following table style. Here, the general format should be: %(flags) (width) (.precision)specifiers.

Sub-SpecifiersBrief
widthThe number of characters required to print
.precisionThe number digits following the decimal points
flags“-:” Justifies the print from the left 
 “+:” Justifies the print from the right
 “0:” It pads the output with 0’s if
 there are fewer characters than the width
 Space: To print a preceding distance between the positive values. 

Integer sub-specifiers formatting

If the output is related to Integer point formatting, Then we should use the following table style. Here, the general format should be %(flags) (width) specifiers.

Sub-SpecifiersBrief
widthThe number of characters required to print
flags“-:” Justifies the print from the left 
 “+:” Justifies the print from the right
 “0:” It pads the output with 0’s if
there are fewer characters than the width
 Space: To print a preceding distance between the positive values. 

String sub-specifiers formatting

Sub-SpecifiersBrief
widthThe number of characters required to print,
if the string has more characters, then the width will not be cut if the series has fewer than it will fill the spaces.
.precisionIt specifies the
 maximum number of characters to be printed. If there are more characters, then the width will be truncated.
flags“-:” Justifies the specified width output
 and padding the work with spaces.  

If the output is related to Strongpoint formatting, Then we should use the following table style. Here, the general format should be: %(flags) (width) (.pecision)specifiers.

Example:

package main;
public class First{
    public static void main( String args[] ) {
System.out.println("The list of today's example");
String n1 = "Mango";
int q1 = 500;
double p1 = 1.2;
String n2 = "Tomato";
int q2 = 1;
double p2 = 0.8;
String n3 = "Juice";
int q3 = 1;
double p3 = 4.5;
String n4 = "Bread";
int q4 = 5;
double p4 = 2.2;
System.out.printf("%nItems\tQuantity\tPrice");
System.out.printf("%n-----\t--------\t-----%n");
System.out.printf("%-8s %-5d \t\t$%7.2f%n", n1, q1, p1);
System.out.printf("%-8s %-5d \t\t$%7.2f%n", n2, q2, p2);
System.out.printf("%-8s %-5d \t\t$%7.2f%n", n3, q3, p3);
System.out.printf("%-8s %-5d \t\t$%7.2f%n", n4, q4, p4);
 }  }

Output:

sub-specifers - Java Format Specifier

Here, we usually used the sub-specifiers to beautify the list. \t is for the four spaces shortcut. We took in a total of 8 spaces before the dollar sign. On the other hand, we used %n for line separator. You can try your example in your PC. Let us know if you got it nicely. We will always be here with you to help you with the best suggestion.

This Post Has One Comment

Leave a Reply