Java IO Tutorial - Java Console.printf(String format, Object ... args)








Syntax

Console.printf(String format, Object ... args) has the following syntax.

public Console printf(String format,  Object ... args)

Example

In the following code shows how to use Console.printf(String format, Object ... args) method.

/*from w  ww.  j a v  a  2s  . c  o  m*/
import java.io.Console;

public class Main {
  public static void main(String[] args) throws Exception {

    Console cnsl = System.console();

    if (cnsl != null) {
      String fmt = "%1$4s %2$10s %3$10s%n";

      // format
      cnsl.printf(fmt, "Items", "Quanity", "Price");
      cnsl.printf(fmt, "-----", "-----", "-----");
      cnsl.printf(fmt, "PHP", "1Kg", "15");
      cnsl.printf(fmt, "CSS", "2Kg", "50");
      cnsl.printf(fmt, "Java", "3Kg", "30");
      cnsl.printf(fmt, "HTML", "4Kg", "80");
    }

  }
}

The code above generates the following result.