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. In this article, we will show how to write a Java String format method with an example. The basic syntax of this is as shown below.

Java String.format 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: first parameter is the locale you want to apply, 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 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 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 the user-specified way. Within this example program, the first statement change the user-specified double value to float.

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

The following Java 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