Java - String format() Method



The Java String format() method is used to get the formatted string using the specified locale, format string, and object arguments. If the locale is not specified in the String.format() method, the default locale is used by calling Locale.getDefault() method itself.

In Java, a locale is an object which represents a specific geographical, political, or cultural region. The format() method accepts different parameters such as locale, format, and object that hold the value of locale l, string format, and object argument.

The format() method has two polymorphic variants with different parameters, as shown in the syntax below.

Syntax

Following is the syntax of the Java String format() method −

public static String format(Locale l, String format, Object... args) //first syntax
public static String format(String format, Object... args) // second syntax

Parameters

  • l − This is the locale to apply during formatting. If it is null, then no localization is applied.

  • format − This is a format string.

  • args − This is the argument referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero.

Return Value

This method returns a formatted string using the specified locale, format string, and arguments.

Example

If we pass the locale, format, and object argument as a parameter to this method, it returns a formatted string.

In the following program, we are instantiating the string class with the value “Java Programming”. Then, using the format() method, we are trying to format the current string at the specified locale “Locale.CANADA” and object argument.

import java.lang.*;
import java.util.*;
public class Format {
   public static void main(String[] args) {
     
      //instantiate the string class
      String str = new String("Ramesh");
      System.out.println("The given string is: " + str);
      System.out.println("After formatting the string.....\n" +    String.format(Locale.CANADA, "My name is: %s", str));
   }
}

Output

On executing the above program, it will produce the following result −

The given string is: Ramesh
After formatting the string....
My name is: Ramesh

Example

If we pass the format and object argument as a parameter to the method, this method returns a formatted string.

In the following example, we are creating an object of the string class with the value “TutorialsPoint”. Using the format() method, we are trying to get the formatted string of the current string in the specified format.

import java.lang.*;
import java.util.*;
public class Format {
   public static void main(String[] args) {
      
      // create an object of the string class
      String str = new String("TutorialsPoint");
      
      // initialize the double variable
      Double d = 234.454d;
      System.out.println("The given string is: " + str);
      System.out.println("The given double value is: " + d);
      
      // use format() method
      String new_str;
      String new_str1;
      new_str = String.format("Welcome to the %s", str);
      new_str1 = String.format("The result is: %15f", d);
      System.out.println("The formatted string is: " + new_str);
      System.out.println("The formatted value is: " + new_str1);
   }
}

Output

Following is the output of the above program −

The given string is: TutorialsPoint
The given double value is: 234.454
The formatted string is: Welcome to the TutorialsPoint
The formatted value is: The result is:     234.454000

Example

If the format value is null, the format() method returns a NullPointerException.

In this example, we are creating an instantiating the string class with the value “Hello”. Then, using the format() method, we are trying to get the formatted string of the current string in the specified format null.

import java.lang.*;
import java.util.*;
public class Format {
   public static void main(String[] args) {
      try {
         
         // create a string literal
         String str1 = new String("Hello");
         System.out.println("The given string is: " + str1);
         
         // using format() method
         String new_str = String.format(Locale.CANADA, null, str1);
      } catch(Exception e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

The above program, produces the following results −

The given string is: Hello
java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null
	at java.base/java.util.Formatter.parse(Formatter.java:2717)
	at java.base/java.util.Formatter.format(Formatter.java:2671)
	at java.base/java.util.Formatter.format(Formatter.java:2625)
	at java.base/java.lang.String.format(String.java:4184)
	at com.tutorialspoint.String.Format.main(Format.java:11)
Exception: java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null

Example

If we pass the illegal format syntax to the method, it throws an IllegalFormatException .

In this program, we are creating an object of the string class with the value “Tutorix”. Then, using the format() method, we are trying to get the formatted string of the current string object in the specified format “%d”. Since we are using the “%d” format for the string, the method will throw an exception.

import java.lang.*;
public class Format {
   public static void main(String[] args) {
      try {
         
         // create an object of the string class
         String str = new String("Tutorix");
         System.out.println("The given string is: " + str);
         
         // using format method we pass
         String new_string = String.format("%d", str);
         System.out.println("The new formatted string is: " + new_string);
      } catch(Exception e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

After executing the above program, it will produce the following output −

The given string is: Tutorix
java.util.IllegalFormatConversionException: d != java.lang.String
	at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4442)
	at java.base/java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2963)
	at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2918)
	at java.base/java.util.Formatter.format(Formatter.java:2689)
	at java.base/java.util.Formatter.format(Formatter.java:2625)
	at java.base/java.lang.String.format(String.java:4143)
	at com.tutorialspoint.String.Format.main(Format.java:10)
Exception: java.util.IllegalFormatConversionException: d != java.lang.String
java_lang_string.htm
Advertisements