Java: Escape Sequences and Formatted Outputs

This lesson will go over escape sequences to print “unprintable” characters, formatting outputs, converters, and flags. Let’s get started!

Escape Sequences

The next lesson is escape sequences. While devising new programs, some “unprintable” characters must be printed, including tab and newline. So, adding a \ (backslash) right before such a character will ensure that such characters escape interpretation by the compiler.      

For example, to print a tab, a backslash must be printed before the character ‘t,’ in this manner: \t.

In case backslash is not used, the letter ‘t’ will be displayed. \t is, therefore, a good example of an escape sequence. So, writing this: 

System.out.println("Great\tDay");

Will result in the following output: 

Great    Day

Here are some other commonly used escape sequences:

For printing a newline (\n)

Example:

System.out.println("Good\nEvening");

Output:

Good
Evening

Print just the backslash character (\\)

Example:

System.out.println("\\");

Output:

\

Using double quotes without ending the string (\”) 

Example:

System.out.println("I said \"great\" not gate");

Output:

I said "great" not gate

Formatting Outputs

While the several examples stated till now helped understand println() and print() methods in displaying an output, sometimes programmers may want some more control over their output’s display. For example, writing this 

System.out.println("The answer for 3.79 divided by 3 is " + (3.79/3));

The result would be:

The answer for 3.79 divided by 3 is 1.26333333

However, such precision is not required in most cases. When this is the case, the printf() method can come to the rescue while displaying the users’ output. Although the printf() method is a more complicated method to operate, it will provide the much-needed control over the outputs. The previous example can be formatted in this manner: 

System.out.printf("The result for %.2f divided by %d is %.3f.", 3.79, 3, 3.79/3);

The output for this statement is going to be:

The result for 3.79 divided by 3 is 1.263.

The printf() method requires the addition of one or more arguments. In this example, four arguments were used.  

The first argument was “The result for %.2f divided by %d is %.3f.” which is the string that is being formatted. 

The string contains %.2f, %d, and %.3f, which are known as format specifiers. They act as placeholders, which are substituted by the other arguments that are integrated. The first format specifier (%.2f) is substituted by the first argument (3.79), the second (%d) by the second argument (3), and so on.

Every format specifier begins with a percent sign (%) and ends with a converter (in this example, they are f and d). They inform the compiler as to how each corresponding argument has to be formatted. Flags are the extra information added between the percent sign (%) and the converter. 

The first format specifier in this example is %.2f.

Here, f is the converter. It informs the compiler that the corresponding argument must be taken as a floating-point number (i.e., numbers with decimal places such as float or double). The system will return an error if the programmer attempts to substitute it with a non-floating-point number.

.2 is the flag, informing the compiler that the programmer wants this argument to be displayed with 2 decimal places. Placing 3 instead of 2 would lead the output to become 3.790 as opposed to 3.79.   

Many specifiers can be used in Java. In the next two sections, more converters and flags used in the specifiers will be discussed.

Converters

The integer converter: %d

This converter is used to format integers such as byte, short, int, and long.

Example:

System.out.printf("%d", 24);

Output:

24

Note:

System.out.printf("%d", 30.6); 

will return an error as 30.6 is not an integer.  

Moreover, System.out.printf(“%f,” 7); will also read as an error since 7 is not a floating-point number.

The newline converter: %n

As is obvious from the name, this converter helps move the cursor to the next line

Example

System.out.printf("%d%n%d", 74, 23);

Output:

74
23

Flags

The width flag

This flag specifies the width.

Example 1

System.out.printf("%5d", 89);

Output:

   89

The output has three spaces before the number 89, making the total width 5. 

Example 2

System.out.printf("%5.2f", 7.9);

Output

 7.90

In the output, there is one space behind the number. Therefore, including the decimal point, the width becomes 5.    

The thousands separator flag (,)

Using this flag, numbers can be displayed with a thousands separator

Example 1

System.out.printf("%,d", 78904);

Output:

78,904

Example 2

System.out.printf("%,.3f", 98763.56789);

Output:

98,763.568

Leave a Reply

%d bloggers like this: