Mega Code Archive

 
Categories / Java Tutorial / Development
 

Right justifying integers in fields

public class MainClass  {    public static void main( String args[] )    {        System.out.printf( "%4d\n", 1 );       System.out.printf( "%4d\n", 12 );       System.out.printf( "%4d\n", 123 );       System.out.printf( "%4d\n", 1234 );       System.out.printf( "%4d\n\n", 12345 ); // data too large        System.out.printf( "%4d\n", -1 );       System.out.printf( "%4d\n", -12 );       System.out.printf( "%4d\n", -123 );       System.out.printf( "%4d\n", -1234 ); // data too large        System.out.printf( "%4d\n", -12345 ); // data too large     } } 1 12 123 1234 12345 -1 -12 -123 -1234 -12345