java.lang.String format(Locale l,String format,Object…. args)

Description :

This java tutorial shows how to use the format() method of String class of java.lang package. This method returns a String representation of the result of the object args based on the format specified on the method arguments. This method is locale dependent as specified on the Locale argument.

Method Syntax :

public static String format(Locale l,String format,Object… args)

Parameter Input :

DataType Parameter Description
Locale l This specifies which locale to use in the formatting. If it's null, the default Locale is used
String format Will dictate what is the format of the Object
Object args This object will be formatted based the format

Method Returns :

This format() method of String class change the method argument Object args into specified string format as method argument depending on the Locale specified..

Compatibility Version :

Requires Java 1.5 and up

Exception :

IllegalFormatException

– The IllegalFormatException will be thrown if the argument String format is invalid.

NullPointerException

– This exception will be thrown if and only if the format is null.

Discussion :

The string format method is a static one thus it needs to be called statically like this String.format(locale,format,args). This method is very helpful if our business requirements specified a format required.

Java Code Example :

This example source code demonstrates the use of static method format() of String class. This example simply format a float object to display only 2 decimal point thus the format is %.2f. Let’s take a look on below example source code for better understanding of the usage of format() method. We have used the Locale defined on the variable ENGLISH of Locale class.

package com.javatutorialhq.java.tutorial.string;

import java.util.Locale;

/*
 * Example source code to format a float object
 */

public class StringFormatDemo {

    public static void main(String[] args) {
        // float object declaration
        float sampleFloat = 1.321234f;
        // declare the Locale to use
        Locale l = Locale.ENGLISH;
        // format our float object
        String strValue = String.format(l,"%.2f", sampleFloat);
        // Printing the formatted object
        System.out.println(strValue);
    }

}

Sample Output :

Running the format() method example source code of String class will give you the following output

1.32

Exception Scenario :

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '.'
	at java.util.Formatter.checkText(Unknown Source)
	at java.util.Formatter.parse(Unknown Source)
	at java.util.Formatter.format(Unknown Source)
	at java.util.Formatter.format(Unknown Source)
	at java.lang.String.format(Unknown Source)
	at com.teknoscope.java.tutorial.string.StringFormatDemo.main(StringFormatDemo.java:14)

Exception in thread "main" java.lang.NullPointerException
	at java.util.regex.Matcher.getTextLength(Unknown Source)
	at java.util.regex.Matcher.reset(Unknown Source)
	at java.util.regex.Matcher.(Unknown Source)
	at java.util.regex.Pattern.matcher(Unknown Source)
	at java.util.Formatter.parse(Unknown Source)
	at java.util.Formatter.format(Unknown Source)
	at java.util.Formatter.format(Unknown Source)
	at java.lang.String.format(Unknown Source)
	at com.teknoscope.java.tutorial.string.StringFormatDemo.main(StringFormatDemo.java:14)

Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer
	at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
	at java.util.Formatter$FormatSpecifier.printFloat(Unknown Source)
	at java.util.Formatter$FormatSpecifier.print(Unknown Source)
	at java.util.Formatter.format(Unknown Source)
	at java.util.Formatter.format(Unknown Source)
	at java.lang.String.format(Unknown Source)
	at com.teknoscope.java.tutorial.string.StringFormatDemo.main(StringFormatDemo.java:14)

Similar Method :

  • java.lang.String format(String format,Object… args)

Suggested Reading List :

References :