Printf and Sprintf for Java

 

Examples

【 printf() and sprintf() 】は以下のような使い方をします。

Example1.java
package examples;


import com.sabaji.format.*;
import java.lang.*;


public class examples1 {
  public static void main(String[] args) {
    String s="[%-10s][%05d][%03.2f]";

    String[]  name=new String[5];
    int[]     year=new int[5];
    double[]  height=new double[5];

    int loop=0;
    name[loop]="Kato";  year[loop]=12;  height[loop]=110.123456; loop++;
    name[loop]="Goto";  year[loop]=24;  height[loop]=123.4567;   loop++;
    name[loop]="Ito";   year[loop]=48;  height[loop]=120.35;     loop++;
    name[loop]="Okuda"; year[loop]=100; height[loop]=110.1;      loop++;
    name[loop]="Sakura";year[loop]=33;  height[loop]=1234567.00; loop++;

    Params p=new Params();
    Format.printf( "[%-10s] [%4s] [%6s]\n", p.add("NAME").add("Year").add("Height") );

    for( loop=0; loop!=name.length; loop++ ){
      p.clear();
      Format.printf( "[%-10s] [%4d] [%3.2lf]\n",
          p.add(name[loop])
           .add(new Integer( year[loop] ) )
           .add(new Double( height[loop]) )
      );
    }
  }
}

 

Example2.java

package examples;
import com.sabaji.format.*;
import java.lang.*;

public class examples2 { public static void main(String[] args) { Params p = new Params();
for(int i=0; i<=130; i+=3){ Integer ii=new Integer(i); p.clear().add(ii).add(ii).add(ii);
Format.printf("%3d. %#4x %2X\n", p ); } } }


© 2002 Sabaji, Inc. All rights reserved.