Formatter out() method in Java with Examples
The out() method is a built-in method of the java.util.Formatter which returns the destination for the output by the formatter.
Syntax:
public Appendable out()
Parameters: The function accepts no parameter.
Return Value: The function returns the destination for the output.
Exceptions: The function throws FormatterClosedException if the formatter has been closed before the function call.
Below is the implementation of the above function:
Program 1:
import java.util.Formatter;
import java.util.Locale;
public class Main {
public static void main(String[] args)
{
StringBuffer buffer
= new StringBuffer();
Formatter frmt
= new Formatter(buffer,
Locale.CANADA);
String name = "My name is Gopal Dave" ;
frmt.format( "What is your name? \n%s !" ,
name);
System.out.println(frmt);
System.out.println( "\nDestination: "
+ frmt.out());
}
}
|
Output:
What is your name?
My name is Gopal Dave !
Destination: What is your name?
My name is Gopal Dave !
Program 2:
import java.util.Formatter;
import java.util.Locale;
public class Main {
public static void main(String[] args)
{
try {
StringBuffer buffer
= new StringBuffer();
Formatter frmt
= new Formatter(buffer,
Locale.CANADA);
String name = "My name is Gopal Dave" ;
frmt.format( "What is your name? \n%s !" ,
name);
System.out.println(frmt);
frmt.close();
System.out.println( "\nDestination: "
+ frmt.out());
}
catch (Exception e) {
System.out.println( "Exception is: "
+ e);
}
}
}
|
Output:
What is your name?
My name is Gopal Dave !
Exception is: java.util.FormatterClosedException
Reference: https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html#out()
Last Updated :
01 Apr, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...