Java String format Method

The Java format method is one of the String Methods, which is to return a formatted string using the user-specified version and arguments. This article will show how to write a Java String format method with an example. The basic syntax of this is shown below.

Java String format method Syntax

The Java Programming Language provides two different methods to format the given string. The following method accepts two parameters: the first parameter is the format we want to apply, and the second is the arguments referenced by the specifier.

public static String format (String format, Object.. args);  // It will return String as output 

//In order to use in program
String.format (String format, Object.. args);

The following Java string.format method accepts three parameters: the first parameter is the locale you want to apply, and the second parameter is the format you want to apply. The third argument is the args referenced by a specifier.

public static String format (locale l, String format, Object.. args);

//In order to use in program
String.format (locale l, String format, Object.. args);
  • Locale: This is the locale we want to apply during the process. If the locale is NULL, then no localization is applied.
  • Format: Please specify the string. For example, %d, %f, %s etc.
  • Args: Arguments referenced by the format specifier. If we specify more arguments, the extra arguments are ignored.

IllegalFormatException: If the string contains an illegal syntax, then it will throw this error. The illegal syntax may include:

  • When the specifier is incompatible with the given arguments.
  • Or when we specify insufficient arguments.

NullPointerException: If the format is NULL.

TIP: If we omit the Locale argument, then Javac will take the system default localization.

Java String.format Example

In this program, we use the String method to change the given object using a user-specified way. Within this example program, the first statement changes the user-specified double value to float.

The third String Function code in Java will style the 9876 value to a positive integer. And the next statement will style the 2406 value to a Negative integer. Then, we used the data value.

The following statement formats 240.9876 value to String data. The last statement is using the specified local (i.e., ENGLISH).

package StringFunctions;
import java.util.Date;
import java.util.Locale;

public class FormatMethod {

	public static void main(String[] args) {
		double dbl = 98765.457896;
		
		String str1 = String.format("%f", dbl);
		System.out.println(str1);
		
		String str2 = String.format("%,2.4f", dbl);
		System.out.println(str2);
		
		String str3 = String.format("%d", 9876);
		System.out.println(str3);
		
		String str4 = String.format("-%d", 2406);
		System.out.println(str4);
		
		String str5 = String.format("Today is %tD", new Date());
		System.out.println(str5);
		
		String str6 = String.format("%s", 240.9876);
		System.out.println(str6);
		
		String str = "Tutorial";
		String std = String.format(Locale.ENGLISH, "%s Gateway",str);
		System.out.println(std);
	}
}
Java String.format Method 1