Create Table of squares and cubes with Formatter in Java

Description

The following code shows how to create Table of squares and cubes with Formatter.

Example


// w  w  w .  ja  v a 2s  .com
import java.util.*; 
 
public class Main { 
  public static void main(String args[]) { 
    Formatter fmt; 
  
    for(int i=1; i <= 10; i++) { 
      fmt = new Formatter(); 
 
      fmt.format("%4d %4d %4d", i, i*i, i*i*i); 
      System.out.println(fmt); 
    } 
 
  } 
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Data Format »




Java Formatter
Java Number Formatter