Java I/O How to - Format Console output








Question

We would like to know how to format Console output.

Answer

/*from  w  ww  .ja  v  a 2  s  .co m*/
import java.io.Console;
import java.sql.SQLException;

public class Main {
  public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Console console = System.console();
    if (console == null) {
      System.err.println("sales: unable to obtain console");
      return;
    }

    console.printf("%s ", "string");
  }
}

The code above generates the following result.