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





Java scanf

Version history

Current version

Version 1.6 [2002-08-29]

New features:
  • Added format flag %u for emulating unsigned byte's, short's, and int's. Example: "%u" with (byte)-1 would give "255" and with (short)-1 it would give "65535". Note that internally Java does not support unsigned numbers. Also be aware that "%u" is not supported for the long data type. If tried an java.lang.ClassCastException is thrown.
Exception handling updates:
  • All calls to System.exit(), which was called with an standard error message in cases where a format flag was used with an incompatible data type, has been replaced by java.lang.ClassCastException, which inherits from java.lang.RuntimeException meaning that there is no need for a try-catch statement or to declare it in the throws declaration. If not caught such an exception will basically has the effect as a System.exit() call. This gives the developer to decide if he/she wants to catch the exception or not. This modification was first requested by Thomas Hartmann, Germany, but thanks to everyone for the suggestion.
  • com.braju.format.ParseErrorException is now extending java.lang.Runtime (instead of java.lang.Exception). More importantly it is now thrown by all Format.*printf() methods. In previous versions such exceptions were caught by the Format.*printf() methods and an error message was send to System.err instead. This update was done in the same spirit as of removing all System.exit() calls.
Bugs fixed:
  • When formatting the values 1e3, 1e5, 1e7 and so on (odd exponential) using the "%e" flag the result was "10.000000e+02", "10.000000e+04", "10.000000e+06" respectively. The output was formally correct, but it did not follow the "%e" definition. The test cases missed since they only tested values with even exponential, e.g. 1e2, 1e4 and so on. Thanks Frode Meling, Norway, for this bug report.

(same version) [2001-11-29]

Javadoc updates:
  • From version 1.5, a few static fields and and few methods where incorrectly included in the javadoc pages. These fields and methods belong to a super class and are not public. They have now been removed from the javadoc documentation. This was due to a small difference in behavior in javadoc v1.3 compared to v1.2. Thanks Frode Meling, Norway, for telling me about this.

Version 1.5 [2000-11-10]

New features:
  • There is now a small command line version of the printf package, which might be useful if you need a printf method in your script language, e.g. MSDOS. It also provides a very fast way of trying out different format strings since you do not have to recompile your Java code each time. Type java com.braju.format.Format -help to get more information.
Bugs fixed:
  • One bug has been fixed. sprintf("%.3e", p.add(4.270E+06)) was incorrectly outputting "4.2610e+06" instead of "4.270e+06". Note that this bug was due to numerical precision problems, and happened only for numbers with very specific bit-patterns. Thank you Lyn Greenhill, US, for the bug report.
Important update:
  • The package name has been changed from hb.format to com.braju. This affects all users of version 1.4 and before. The reason for this change was that, as many have been pointing out, the package name hb.format did not follow the Java naming conventions. I registered the domain and now you can be sure that the package name com.braju is unique.

Previous versions

(same version) [1999-03-08]

  • All files are now with zip-extension instead of jar-extension. Note that it is just the filenames that are changed; the files are still created with the jar-tool. The reason for this change is that many Netscape users have experienced problems with the browser trying to convert the jar-archive during download.
  • The javadoc documentation is now created with the new Java 2-style.

Version 1.4 [1998-10-14]

Bugs fixed:
  • A bug has been corrected. sprintf("%9.2e", p.add(-9.996)) was incorrectly outputting "-10e+00" instead of "-1.00e+01". Yoshinori Mizuno, Japan, was the first one to report this bug. A consequence of this bug was that in some cases the %g-format gave incorrect number of significants.
Other updates:
  • The package is now a compressed jar-archive, which means almost half the size compared to previous versions. Previous versions were also in jar-archive, but they were not compressed since some of the early releases of the jar-tool did not generate fully cross platform compatible archives. JDK 1.1.7 does hopefully work better in this case.

Version 1.3.1 [1998-08-20]

Other updates:
  • JDK 1.2beta4 complained when using hb.format package version 1.3. Messages like "Exception... (Local variable name has bad constant pool index)..." did show up. A simple recompilation fixed this problem!

Version 1.3 [1998-02-23]

New features:
  • Now support for parameter-calls with an array of objects, i.e. Object[].
  • The data types byte and short are supported in the Java 1.0.2 version also. For more information see API about hb.lang.Byte and hb.lang.Short.
Other updates:
  • IMPORTANT! The class Parameters does not inherit from java.util.Vector anymore, because it was not compatible with Java1.2beta2 (see below). This is a necessary modification and the sooner it is done the less problems you will have. I am sorry if some of you have to change your source because of this, but I am pretty sure most of you won't need to do it.
Bugs fixed:
  • This version is also compatible with Java 1.2beta2. Previous versions could not compile with JDK1.2beta2, due to new methods in the interface of java.lang.Vector. Thank you Delaigue Pascal, France, for making me aware of this problem.
  • The fprintf-method did not work properly with Java 1.0.2.
  • When format-string contained e.g. "foo\nbar\r\n" and the line separator of the system was "\r\n" (as on MS-DOS) the result was changed to "foo\r\nbar\r\r\n" (one extra '\r'). In this new version the output is as expected: "foo\r\nbar\r\n". This problem could show up if one first used sprintf and the used the resulting string in another call to a Xprintf-method.

Version 1.2.2 [1998-02-11]

Bugs fixed:
  •  In some situations the Java 1.0.2 version of the package called a Java 1.1 method, i.e. an applet would not run in a browser only supporting Java 1.0.2. Sorry for that! Thank you Michinori.Sakuma, Japan, for this bug report.

Version 1.2.1 [1998-02-02]

New features:
  • printf("%s", p.add(obj)) will print "null" if object is null. This is how System.out.print() works.

Version 1.2 [1998-01-15]

New features:
  • There is two versions available; one Java 1.0.2 (i.e. for applets in browsers only supporting Java 1.0.2) and one Java 1.1.
  • Parameters-class is now supporting byte and short.
  • Xprintf-methods can now print booleans, e.g. TRUE, false, t, FA etc.
  • Xprintf-methods can now print binaries, e.g. 0b101, 00010110, 11100110 etc.
  • Support of printing signed values as binary, hexadecimal and octal format. The default width for a negative number will then be 2 for byte, 4 for short, 8 for integer and 16 for long (note byte and short is not supported by the Java 1.0.2 version). E.g.:
        printf("%#x", p.add((byte)-2)) gives "0xfe"
        printf("%#x", p.add((short)-2)) gives "0xfffe"
        printf("%#x", (p.add(int)-2)) gives "0xfffffffe"
  • if "%s" is used and corresponding parameter is not a String then the toString()-method of the parameter-object is used.
  • %#c will print a character in UTF-code, e.g. %#c gives "\u0041" for 'A'.
  • Xprintf now scans the format-string for "\n" and will convert it to what is correct on the local system, i.e. the system property line.separator. Two new method getModifyLineSeparators() and setModifyLineSeparators().
Bugs fixed:
  • printf("%l", p.add(Long.MIN_VALUE)) gave "--9223372036854775808" (an extra sign).
  • printf("%f", p.add(d)) missed the sign when -0.999... < d < -0.000...
  • printf("%-12.4e", p.add(-0.123456)) gave incorrectly "-1.2346 e-01" (wrong padding).
Other updates:
  • Format.printf("%1.4g", p.add(0.000000001)) was printing 0.0000 but should print 1.0000e-09 according to ANSI-C. My C-book told me the first alternative was the correct way. Thanks to Clinton Knight, Georgia Institute of Technology, things work better now.

Version 1.1 [1997-11-20]

Bugs fixed:
  • In the previous version there was a minor but serious bug in the rounding of float numbers. E.g. 42.887 was round correctly to 42.89, but 42.897 was incorrectly rounded to 42.810. This bug is now fixed. Made also some performance improvement, which also reduced the size of the code. Thank you Tervo Salminen, Finland.

Version 1.0 [1997-10-24]

The first public release was on 17:th of November 1997.