Java 中的打印流格式(区域设置、字符串、对象)方法,示例

原文:https://www . geesforgeks . org/print stream-format locale-string-object-in-Java-method-with-examples/

Java 中 【打印流】 类的格式(区域设置、字符串、对象)方法用于使用给定的区域设置打印流中的格式化字符串。字符串使用指定的格式和作为参数传递的参数进行格式化。

语法:

public PrintStream format(本地、字符串格式、对象... args)

参数:该方法接受两个强制参数:

  • 区域设置是应用于该方法的区域设置值
  • 格式是字符串的格式。
  • 参数是格式化字符串的参数数量。它可以是可选的,即根据格式没有参数或任意数量的参数。

返回值:这个方法返回这个 PrintStream 实例。

异常:该方法抛出以下异常:

  • NullPointRexception如果格式为空,则抛出此异常。
  • IllegalFormatException 如果指定的格式不合法或参数不足,则会引发此问题。

以下方法说明了格式(区域设置、字符串、对象)方法的工作原理:

程序 1:

// Java program to demonstrate
// PrintStream format(String, Object) method

import java.io.*;
import java.util.*;

class GFG {
    public static void main(String[] args)
    {

        try {

            // Get the parameters
            Locale locale = Locale.getDefault();

            double arg = 47.65734;

            String format = "GeeksForGeeks %.8f";

            // Create a PrintStream instance
            PrintStream stream
                = new PrintStream(System.out);

            // print the formatted string
            // to this stream using format() method
            stream.format(format, arg);

            stream.flush();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output:

GeeksForGeeks 47.65734000

程序 2:

// Java program to demonstrate
// PrintStream format(String, Object) method

import java.io.*;
import java.util.*;

class GFG {
    public static void main(String[] args)
    {

        try {

            // Get the parameters
            Locale locale = Locale.getDefault();

            String arg1 = "GFG";
            String arg2 = "GeeksforGeeks";

            String format = "A Computer Science "
                            + "Portal  %1$s, %1$s and %2$s";

            // Create a PrintStream instance
            PrintStream stream
                = new PrintStream(System.out);

            // print the formatted string
            // to this stream using format() method
            stream.format(format, arg1, arg2);

            stream.flush();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output:

A Computer Science Portal  GFG, GFG and GeeksforGeeks