braju.com | Java fprintf, printf, sprintf (fscanf, scanf, sscanf) |
HomeDownload Purchase History Examples Documentation API/javadoc FAQ Feedback About Donate Java scanf |
Java printf & scanf (beta version)Welcome to the Java printf & scanf beta version area! In this area you will find a working implementation of Java scanf (and printf) which you can download, a beginners introduction to scanf and a continuously growning compilation of scanf examples. Contents of this beta area
This is a beta version!Even though the library seems to work fine, it is a beta version. Therefore, the original Java printf() package will have the highest priority and will be maintained as a seperate project. The beta version will have lower priority since I am very busy doing my PhD. Note that this library has a totally different internal structure than the original printf library. In addition to the scanf() methods, there is also (totally rewritten) printf() methods implemented in the beta version. The reason for this is that the new scanf() and printf() is based on the same underlying structure. I highly recommend that you use the original printf() package for important printf() calls since it has been tested by thousands of users since 1997. That version is stable and of very high quality! This new one has not undergone such a quality assurance, yet! Current version
Previous versions
One quick exampleThere are two main approaches to let scanf store results in variables: (1) using Java reflection or (2) using Variable objects. The first approach is shown below and the second approach is explained in Introduction to Java scanf and examplified in A lot of scanf examples. 1 import com.braju.beta.format.*; 2 3 public class PersonReflect2 { 4 // Parameters to be used by scanf() with "this" always preset. 5 private Parameters ps = new Parameters(this).mark(); 6 // Parameters to be used by printf(). 7 private Parameters p = new Parameters(); 8 public String name; 9 public int age; 10 11 public void query() throws Exception { 12 Format.printf("Enter your full name: "); 13 Format.scanf(" %[^\n]", ps.add("name")); 14 Format.printf("Enter your age (in years): "); 15 Format.scanf("%i", ps.add("age")); 16 } 17 18 public void display() { 19 Format.printf("Hello %s, you are %i years old.\n", 20 p.add(name).add(age)); 21 } 22 23 public static void main(String args[]) throws Exception { 24 PersonReflect2 currPerson = new PersonReflect2(); 25 currPerson.query(); 26 currPerson.display(); 27 } 28 } Project planThe package has been prepared for a long time (first unofficial version was actually up and running in March 1998). Underlying the complex functions printf() and scanf() are several important classes for buffered input and output reading of datatypes etc. The classes and their APIs will be opened up as the beta version matures. However, the first versions will only give access to the Format.printf(...) and Format.scanf(...) methods and the Parameters class and all the Variable classes. More and more of the underlying classes will be made public as the project evolves. At a later phase, support for Java 1.0.2 might also be given (does anyone actually need this anymore?).
Henrik Bengtsson |
|||||||||||||||||||||
|