Java Format Specifier
In this chapter you will learn:
- What is Format Specifier
- What are the specifiers for Java Formatter class
- Example - Java Format Specifier
- Example - Unknown Format Conversion Exception
- How to use %n and %% specifiers to escape new line and percentage
- Example - The following code combines the %n, %d %% to display file copy progress information
Description
The format()
method accepts a wide variety of format specifiers.
When an uppercase specifier is used, then letters are shown in uppercase.
Otherwise, the upper- and lowercase specifiers perform the same conversion.
Specifier List
The following table shows the format specifiers:
Format Specifier | Conversion Applied |
---|---|
%a %A | Floating-point hexadecimal |
%b %B | Boolean |
%c | Character |
%d | Decimal integer |
%h %H | Hash code of the argument |
%e %E | Scientific notation |
%f | Decimal floating-point |
%g %G | Uses %e or %f, whichever is shorter |
%o | Octal integer |
%n | Inserts a newline character |
%s %S | String |
%t %T | Time and date |
%x %X | Integer hexadecimal |
%% | Inserts a % sign |
If the argument doesn't match the type-checks, an IllegalFormatException
is thrown.
Example
Java Format Specifier
import java.util.Calendar;
import java.util.Formatter;
/*w w w . j ava2 s . c om*/
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt = new Formatter();
System.out.println(fmt.format("%s gap filler %d %f", "Astring", 10, 12.3));
}
}
You can obtain a reference to the underlying output buffer by calling out()
.
It returns a reference to an Appendable
object.
The code above generates the following result.
Example 2
Unknown Format Conversion Exception
import java.util.Date;
import java.util.Formatter;
/*from w w w. java2s.c o m*/
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt.format("%t", new Date());
System.out.println(fmt);
}
}
The code above generates the following result.
Example 3
The %n
and %%
format specifiers escape sequences.
The %n
inserts a newline.
The %%
inserts a percent sign.
You can use the standard escape sequence \n
to embed a newline character.
Here is an example that demonstrates the %n
and %%
format specifiers:
import java.util.Formatter;
/*w w w . j a v a 2 s . c o m*/
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt.format("line%nline %d%% complete", 88);
System.out.println(fmt);
}
}
The output:
Example 4
The following code combines the %n, %d %% to display file copy progress information.
import java.util.Formatter;
//from w ww .jav a2 s.c om
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt.format("Copying file%nTransfer is %d%% complete", 88);
System.out.println(fmt);
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- What is Format Specifier
- How and when to use Uppercase Option for Java Formatter
- How to uppercase hexadecimal
- How to uppercase e symbol
Java Format Specifier
Java Format Specifier UppercaseJava Format Precision
Java Format Flags
Java Format Justifying Output
Java Format Negative and Positive
Java Format Line up Space
Java Format Parentheses
Java Format Zero Padding
Java Format Comma
Java Format Alternate Conversion
Java Format Argument Index
Java Format date time
Java Format Date