-
[SOLVED] [newbie]
A book example...I'm trying to guess...
public class PrintStream
{
public PrintStream printf(String fmt, Object... args);
}
1. Can I convert PrintStream to an object that I can display on screen (ahem...the default stream writer), say, using System.out.println.
2. What values do I need to set for 'fmt'?
:confused:
-
1. Can I convert PrintStream to an object that I can display on screen (ahem...the default stream writer), say, using System.out.println.
System.out is a PrintStream — see Field Detail of the System class.
2. What values do I need to set for 'fmt'?
Here is where the javadocs (aka, documentation) become(s) useful.
Start with Java™ Platform, Standard Edition 6. In the lower left frame find the PrintStream class, select the link and the PrintStream class api loads in the main frame. This document contains a lot of information about this class and how to use it.
Scroll down to the Method Summary to find your two–argument printf method and follow the link to the Method Detail section.
The Parameters section shows where to find out about this with a link into the Formatter class.
An example:
Code:
import java.awt.Point;
import java.io.PrintStream;
public class Test {
public static void main(String[] args) {
PrintStream ps = new PrintStream(System.out);
ps.println("printing with ps");
Point p = new Point(100, 35);
String format = "p = [%d, %d]%n";
Object[] argsArray = { p.x, p.y };
ps.printf(format, argsArray);
// Explore System.out:
System.out.printf("Is System.out a PrintStream: %b%n",
System.out instanceof PrintStream);
// Use methods of the Class class.
System.out.printf("Class name of System.out: %s%n",
System.out.getClass().getName());
System.out.printf("Superclass name: %s%n",
System.out.getClass().getSuperclass().getName());
}
}