braju.com Java fprintf, printf, sprintf (fscanf, scanf, sscanf)

 

Home

Download
Purchase
History
Examples
Documentation
API/javadoc
FAQ
Feedback
About

Donate

Java scanf

Examples

In addition to the examples below there is also some example applets (Java 1.0.2 compatible).

 1 import com.braju.format.*; 
 2 
 3 public class Example01 { 
 4   public static void main(String argv[]) { 
 5     Parameters p = new Parameters(); 
 6 
 7     int SIZE = 4; 
 8     String [] name  = new String[SIZE]; 
 9     int    [] age  = new int[SIZE]; 
10     double [] score = new double[SIZE]; 
11 
12     // Setup database 
13     int n = 0; 
14     name[n] = "Buffalo Bill"; age[n] = 45;  score[n] = 78.43;   n++; 
15     name[n] = "Donald Duck";  age[n] = 58;  score[n] = 97.983;  n++; 
16     name[n] = "Dart Vader";   age[n] = 212; score[n] = 47.3;    n++; 
17     name[n] = "Bill Gates";   age[n] = 40;  score[n] =  1.0201; n++; 
18 
19     // Print contents of database 
20     Format.printf("%-15s %4s %7s\n", p.add("NAME:").add("AGE:").add("SCORE:"));
21     Format.printf("============================\n"); 
22     for(int i=0; <SIZE; i++) 
23       Format.printf("%-15s %4d %6.2f%%\n", p.add(name[i]).add(age[i]).add(score[i]));
24   } 
25 }
Download: Example01.java

 1 import com.braju.format.*; 
 2 
 3 public class Example03 { 
 4   public static void main(String argv[]) { 
 5     for(int i=-10; <10; i++) { 
 6       Format.printf("i=%3i =%-3i =%+3i =% 3i\n", 
 7              new Parameters(i).add(i).add(i).add(i));
 8     } 
 9   } 
10 }
Download: Example03.java

 1 import com.braju.format.*; 
 2 
 3 public class Example04 { 
 4   public static void main(String argv[]) { 
 5     double d = 123.45678901234567890; 
 6     int sign = 1; 
 7 
 8     for(int i=0; <=10; i++) { 
 9       sign *= -1; 
10       d *= sign; 
11       Format.printf("d=%+15.*f =%-15.*f=%f\n", 
12              new Parameters(i).add(d).add(i).add(d).add(d));
13     } 
14   } 
15 }
Download: Example04.java

 1 import com.braju.format.*; 
 2 
 3 public class Example05 { 
 4   public static void main(String argv[]) { 
 5     Parameters p = new Parameters();
 6     for(int i=0; <=130; i+=10)
 7       Format.printf("%3d. %3o %#4x%2X\n", p.add(i).add(i).add(i).add(i));
 8   } 
 9 }
Download: Example05.java

 1 import com.braju.format.*; 
 2 
 3 public class Example06 { 
 4   public static void main(String argv[]) { 
 5     double d; 
 6 
 7     for(int i=-5; <=5; i++) { 
 8       d = 123.45678901234567890 * Math.pow(-10.0,i);
 9       Format.printf("d=%+016f =% 15.4e =%+15.4G\n", 
10              new Parameters(d).add(d).add(d)); 
11     } 
12   } 
13 }
Download: Example06.java

 1 import com.braju.format.*; 
 2 
 3 public class Example07 { 
 4   public static void main(String argv[]) { 
 5     Format.printf("Hello %s!\n", new Parameters("world")); 
 6     Format.printf("Hello %10s!\n", new Parameters("world")); 
 7     Format.printf("Hello %-10.3s!\n", new Parameters("world"));
 8   } 
 9 } 
Download: Example07.java

 1 import com.braju.format.*; 
 2 import java.io.*;
 3 
 4 public class Example08 { 
 5   public static void main(String argv[]) {
 6     Parameters p = new Parameters();
 7 
 8     try {
 9       Writer out = new FileWriter("Example08.out");
10       String s = Format.sprintf("Hello %s!\n", p.add("world"));
11       out.write(s);
12       out.close();
13     } catch (java.io.IOException ex) {
14       System.err.println("Could not write to file.");
15       System.exit(1);
16     }
17   } 
18 } 
Download: Example08.java