- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Formatter format() Method
Description
The Java Formatter format(String format,Object... args) method writes a formatted string to this object's destination using the specified format string and arguments. The locale used is the one defined during the construction of this formatter.
Declaration
Following is the declaration for java.util.Formatter.format() method
public Formatter format(String format,Object... args)
Parameters
format − A format string as described in Format string syntax.
args − Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification
Return Value
This method returns this formatter
Exception
FormatterClosedException − If this formatter has been closed by invoking its close() method
IllegalFormatException − If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions.
Formatting a String with a Variable using Formatter Example
The following example shows the usage of Java Formatter format(String, Object) method to format a string using a formatter. We've created a formatter object with a StringBuffer and a locale. Formatter is used to print a string using the format() method.
package com.tutorialspoint; import java.util.Formatter; import java.util.Locale; public class FormatterDemo { public static void main(String[] args) { // create a new formatter StringBuffer buffer = new StringBuffer(); Formatter formatter = new Formatter(buffer, Locale.US); // format a new string String name = "World"; formatter.format("Hello %s !", name); // print the formatted string System.out.println(formatter); formatter.close(); } }
Output
Let us compile and run the above program, this will produce the following result −
Hello World !
Formatting a String with two Variables using Formatter Example
The following example shows the usage of Java Formatter format(String, Object, Object) method to format a string using a formatter. We've created a formatter object with a StringBuffer and a locale. Formatter is used to print a string using the format() method.
package com.tutorialspoint; import java.util.Formatter; import java.util.Locale; public class FormatterDemo { public static void main(String[] args) { // create a new formatter StringBuffer buffer = new StringBuffer(); Formatter formatter = new Formatter(buffer, Locale.US); // format a new string String name = "Robert"; int rollNo = 10; formatter.format("Name = %s, RollNo = %d", name, rollNo); // print the formatted string System.out.println(formatter); formatter.close(); } }
Output
Let us compile and run the above program, this will produce the following result −
Name = Robert, RollNo = 10
Formatting a String with three Variables using Formatter Example
The following example shows the usage of Java Formatter format(String, Object, Object, Object) method to format a string using a formatter. We've created a formatter object with a StringBuffer and a locale. Formatter is used to print a string using the format() method.
package com.tutorialspoint; import java.util.Formatter; import java.util.Locale; public class FormatterDemo { public static void main(String[] args) { // create a new formatter StringBuffer buffer = new StringBuffer(); Formatter formatter = new Formatter(buffer, Locale.US); // format a new string String name = "Robert"; int rollNo = 10; String section = "A"; formatter.format("Name = %s, RollNo = %d, Section = %s", name, rollNo, section); // print the formatted string System.out.println(formatter); formatter.close(); } }
Output
Let us compile and run the above program, this will produce the following result −
Name = Robert, RollNo = 10, Section = A
To Continue Learning Please Login
Login with Google