Java fprintf, printf, sprintf (fscanf, scanf, sscanf)





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

Version: (date: 2001-06-27)
Phase: 2 (see below)
Status: Made the FormatReader, FormatWriter and FormatString class public on request. No bugs reported from previous version!

Previous versions

Version: (date: 2001-02-05, first public)
Phase: 1 (see below)
Status: First public release! Even though I have tested this version for a while, I will (from previous experiences) expect that people will find bugs and come with suggestions how to improve the package.

One quick example

There 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.*;
 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;
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   }
18   public void display() {
19     Format.printf("Hello %s, you are %i years old.\n", 
20       p.add(name).add(age));
21   }
23   public static void main(String args[]) throws Exception {
24     PersonReflect2 currPerson = new PersonReflect2();
25     currPerson.query();
26     currPerson.display();
27   }
28 }

Project plan

The 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?).

Phase: Description: Dates:
2 This phase also opens up the API for FormatReader, FormatWriter and FormatString. See also phase 1. 2001-06-27 --
1 This phase opens up the API for Format.printf(...) and Format.scanf(...), the Parameters class and all the needed Variable classes. Using feedback from beta tester, the functionality of scanf() and also printf() will be tested and conformed to the ANSI-C standard. At this phase only Java 1.1 and later is supported. An important aspect is also how to handle Currently such exceptions are caught internally and can be access if one would like to. I need comments on this. 2001-02-05 -- 2001-06-27

Henrik Bengtsson email.