braju.com
Java printf & scanf (beta)

com.braju.beta.format
Class Format

java.lang.Object
  |
  +--com.braju.beta.format.Format

public class Format
extends java.lang.Object

Provides true C-style printf(), scanf() etc functionality with any number of parameters.

This class is all static and there is no reason for making an instance of it.
For more information on how to construct a printf- och scanf-format string, please see documentation about FormatWriter and FormatReader.

Example 1 (only Java 1.1 and above)

import com.braju.format.*;

public class Person {
  // In Java 1.1 and above we can make use of reflection. To do currPerson you
  // have to put your variables in a PUBLIC class and make them PUBLIC.
  // Note that they can not be hidden inside a method, they have to be
  // attributes.
 
  public String name;
  public int age;

  public void query() throws Exception {
    Format.printf("Enter your full name: ");
    // Note that the variable names to FormatReader must be inside quotas,
    // i.e. given as a String. The first parameter tells FormatReader in
    // which object the variables are located. In this case "this".
    Format.scanf(" %[^\n]", new Parameters(this).add("name"));
    Format.printf("Enter your age (in years): ");
    Format.scanf("%i", new Parameters(this).add("age"));
  }

  public void display() {
    Format.printf("Hello %s, you are %i years old.\n",
      new Parameters(name).add(age));
  }

  public static void main(String args[]) throws Exception {
    Person currPerson = new Person();
    currPerson.query();
    currPerson.display();
  }
}

Note that if your program are supposed to run under Java1.0.2 you should use com.braju.format.Format102 instead. It works the similar.

See Also:
Format102, FormatInputStream, FormatOutputStream, FormatReader, FormatWriter, Parameters, Variable

Field Summary
static FormatWriter err
          Formatted writing to the system standard error stream is available through this FormatWriter [...].
static FormatReader in
          Formatted reading from the system standard input stream is available through this FormatReader [...].
static FormatWriter out
          Formatted writing to the system standard output stream is available through this FormatWriter [...].
 
Method Summary
static int fprintf(java.io.Writer out, FormatString fmtstr, java.lang.Object[] obj)
          Provides direct printf-formatted writing to a Writer with format specified in a String [...].
static int fprintf(java.io.Writer out, FormatString fmtstr, Parameters parameters)
          Provides printf-formatted writing to a Writer with format specified by a (precompiled) FormatString [...].
static int fprintf(java.io.Writer out, java.lang.String fmt)
          Provides direct printf-formatted writing to a Writer with format specified in a String [...].
static int fprintf(java.io.Writer out, java.lang.String fmt, java.lang.Object[] obj)
          Provides direct printf-formatted writing to a Writer with format specified in a String [...].
static int fprintf(java.io.Writer out, java.lang.String fmt, Parameters parameters)
          Provides direct printf-formatted writing to a Writer with format specified in a String [...].
static int fscanf(FormatReader in, FormatString fmtstr, Parameters parameters)
          Provides scanf-formatted reading from a Reader with format specified by a (precompiled) FormatString [...].
static int fscanf(FormatReader in, java.lang.String fmt, Parameters parameters)
          Provides direct scanf-formatted reading from a Reader with format specified by a String [...].
 java.lang.Exception getInternalException()
           
static java.lang.String getVersion()
           
static void main(java.lang.String[] args)
          This is the command line version of Java printf.
static int printf(FormatString fmtstr, java.lang.Object[] obj)
          Provides printf-formatted writing to the system standard output stream with format specified by a (precompiled) FormatString [...].
static int printf(FormatString fmtstr, Parameters parameters)
          Provides printf-formatted writing to the system standard output stream with format specified by a (precompiled) FormatString [...].
static int printf(java.lang.Object o)
          Provides direct printf-formatted writing the specified Object to the system standard output using the toString()-method of the Object [...].
static int printf(java.lang.String s)
          Provides direct printf-formatted writing the specified String to the system standard output [...].
static int printf(java.lang.String fmt, java.lang.Object[] obj)
          Provides direct printf-formatted writing to the system standard output stream with format specified in a String [...].
static int printf(java.lang.String fmt, Parameters parameters)
          Provides direct printf-formatted writing to the system standard output stream with format specified in a String [...].
static int scanf(FormatString fmtstr, Parameters parameters)
          Provides scanf-formatted reading from the system standard input stream with format specified by a (precompiled) FormatString [...].
static int scanf(java.lang.String fmt)
          Provides direct scanf-formatted reading from the system standard input stream with format specified by a String [...].
static int scanf(java.lang.String fmt, Parameters parameters)
          Provides direct scanf-formatted reading from the system standard input stream with format specified by a String [...].
static java.lang.String sprintf(FormatString fmtstr, java.lang.Object[] obj)
          Provides printf-formatted writing to a String with format specified by a (precompiled) FormatString.
static java.lang.String sprintf(FormatString fmtstr, Parameters parameters)
          Provides printf-formatted writing to a String with format specified by a (precompiled) FormatString [...].
static java.lang.String sprintf(java.lang.String fmt, java.lang.Object[] obj)
          Provides direct printf-formatted writing to a String with format specified in a String [...].
static java.lang.String sprintf(java.lang.String fmt, Parameters parameters)
          Provides direct printf-formatted writing to a String with format specified in a String [...].
static int sscanf(java.lang.String source, FormatString fmtstr, Parameters parameters)
          Provides scanf-formatted reading of a String with format specified by a (precompiled) FormatString [...].
static int sscanf(java.lang.String source, java.lang.String fmt, Parameters parameters)
          Provides direct scanf-formatted reading of a String with format specified by a String [...].
 void throwInternalException()
           
static void version()
          Print, on standard output, information about this class.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

in

public static FormatReader in
Formatted reading from the system standard input stream is available through this FormatReader [...]. It is an analog to System.in.

out

public static FormatWriter out
Formatted writing to the system standard output stream is available through this FormatWriter [...]. It is an analog to System.out.

err

public static FormatWriter err
Formatted writing to the system standard error stream is available through this FormatWriter [...]. It is an analog to System.err.
Method Detail

getInternalException

public java.lang.Exception getInternalException()

throwInternalException

public void throwInternalException()
                            throws java.lang.Exception

fprintf

public static int fprintf(java.io.Writer out,
                          FormatString fmtstr,
                          Parameters parameters)
Provides printf-formatted writing to a Writer with format specified by a (precompiled) FormatString [...]. If you are using the same format specification over and over again, e.g. in a for-loop, you can improve performance if you precompile your format string. See the method compileFormatString(String) in FormatOutputStream.
Returns:
the number of characters written. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

fprintf

public static int fprintf(java.io.Writer out,
                          java.lang.String fmt,
                          Parameters parameters)
Provides direct printf-formatted writing to a Writer with format specified in a String [...].
Returns:
the number of characters written. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

fprintf

public static int fprintf(java.io.Writer out,
                          FormatString fmtstr,
                          java.lang.Object[] obj)
Provides direct printf-formatted writing to a Writer with format specified in a String [...].
Returns:
the number of characters written. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

fprintf

public static int fprintf(java.io.Writer out,
                          java.lang.String fmt,
                          java.lang.Object[] obj)
Provides direct printf-formatted writing to a Writer with format specified in a String [...]. If you are using the same format specification over and over again, e.g. in a for-loop, you can improve performance if you precompile your format string. See the method compileFormatString(String) in FormatOutputStream.
Returns:
the number of characters written. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

fprintf

public static int fprintf(java.io.Writer out,
                          java.lang.String fmt)
Provides direct printf-formatted writing to a Writer with format specified in a String [...]. No parameters used/needed. If you are using the same format specification over and over again, e.g. in a for-loop, you can improve performance if you precompile your format string. See the method compileFormatString(String) in FormatOutputStream.
Returns:
the number of characters written. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

printf

public static int printf(FormatString fmtstr,
                         Parameters parameters)
Provides printf-formatted writing to the system standard output stream with format specified by a (precompiled) FormatString [...].
Returns:
the number of characters written. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

printf

public static int printf(java.lang.String fmt,
                         Parameters parameters)
Provides direct printf-formatted writing to the system standard output stream with format specified in a String [...]. If you are using the same format specification over and over again, e.g. in a for-loop, you can improve performance if you precompile your format string. See the method compileFormatString(String) in FormatOutputStream.
Returns:
the number of characters written. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

printf

public static int printf(FormatString fmtstr,
                         java.lang.Object[] obj)
Provides printf-formatted writing to the system standard output stream with format specified by a (precompiled) FormatString [...].
Returns:
the number of characters written. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

printf

public static int printf(java.lang.String fmt,
                         java.lang.Object[] obj)
Provides direct printf-formatted writing to the system standard output stream with format specified in a String [...]. If you are using the same format specification over and over again, e.g. in a for-loop, you can improve performance if you precompile your format string. See the method compileFormatString(String) in FormatOutputStream.
Returns:
the number of characters written. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

printf

public static int printf(java.lang.String s)
Provides direct printf-formatted writing the specified String to the system standard output [...]. No Parameters used/needed. Compare this method with the System.out.print()-method and note that in the former case "%%" will be printed as "%".
Returns:
the number of characters written. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

printf

public static int printf(java.lang.Object o)
Provides direct printf-formatted writing the specified Object to the system standard output using the toString()-method of the Object [...]. No parameters used/needed. Compare this method with the System.out.print()-method and note that in the former case "%%" will be printed as "%".
Returns:
the number of characters written. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

sprintf

public static java.lang.String sprintf(FormatString fmtstr,
                                       Parameters parameters)
Provides printf-formatted writing to a String with format specified by a (precompiled) FormatString [...].

sprintf

public static java.lang.String sprintf(java.lang.String fmt,
                                       Parameters parameters)
Provides direct printf-formatted writing to a String with format specified in a String [...]. If you are using the same format specification over and over again, e.g. in a for-loop, you can improve performance if you precompile your format string. See the method compileFormatString(String) in FormatOutputStream.

sprintf

public static java.lang.String sprintf(FormatString fmtstr,
                                       java.lang.Object[] obj)
Provides printf-formatted writing to a String with format specified by a (precompiled) FormatString.

sprintf

public static java.lang.String sprintf(java.lang.String fmt,
                                       java.lang.Object[] obj)
Provides direct printf-formatted writing to a String with format specified in a String [...]. If you are using the same format specification over and over again, e.g. in a for-loop, you can improve performance if you precompile your format string. See the method compileFormatString(String) in FormatInputStream.

fscanf

public static int fscanf(FormatReader in,
                         FormatString fmtstr,
                         Parameters parameters)
                  throws ParseException,
                         java.io.IOException,
                         java.lang.NoSuchFieldException,
                         java.lang.IllegalAccessException
Provides scanf-formatted reading from a Reader with format specified by a (precompiled) FormatString [...]. If you are using the same format specification over and over again, e.g. in a for-loop, you can improve performance if you precompile your format string. See the method compileFormatString(String) in FormatInputStream.
Returns:
the number of variables (format flags) read. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

fscanf

public static int fscanf(FormatReader in,
                         java.lang.String fmt,
                         Parameters parameters)
                  throws ParseException,
                         java.io.IOException,
                         java.lang.NoSuchFieldException,
                         java.lang.IllegalAccessException
Provides direct scanf-formatted reading from a Reader with format specified by a String [...]. If you are using the same format specification over and over again, e.g. in a for-loop, you can improve performance if you precompile your format string. See the method compileFormatString(String) in FormatInputStream.
Returns:
the number of variables (format flags) read. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

scanf

public static int scanf(FormatString fmtstr,
                        Parameters parameters)
Provides scanf-formatted reading from the system standard input stream with format specified by a (precompiled) FormatString [...]. If you are using the same format specification over and over again, e.g. in a for-loop, you can improve performance if you precompile your format string. See the method compileFormatString(String) in FormatInputStream.
Returns:
the number of variables (format flags) read. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

scanf

public static int scanf(java.lang.String fmt,
                        Parameters parameters)
Provides direct scanf-formatted reading from the system standard input stream with format specified by a String [...]. If you are using the same format specification over and over again, e.g. in a for-loop, you can improve performance if you precompile your format string. See the method compileFormatString(String) in FormatInputStream.
Returns:
the number of variables (format flags) read. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

scanf

public static int scanf(java.lang.String fmt)
Provides direct scanf-formatted reading from the system standard input stream with format specified by a String [...]. No parameters used/needed.
Returns:
the number of variables (format flags) read. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

sscanf

public static int sscanf(java.lang.String source,
                         FormatString fmtstr,
                         Parameters parameters)
Provides scanf-formatted reading of a String with format specified by a (precompiled) FormatString [...]. If you are using the same format specification over and over again, e.g. in a for-loop, you can improve performance if you precompile your format string. See the method compileFormatString(String) in FormatInputStream.
Returns:
the number of variables (format flags) read. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

sscanf

public static int sscanf(java.lang.String source,
                         java.lang.String fmt,
                         Parameters parameters)
Provides direct scanf-formatted reading of a String with format specified by a String [...]. If you are using the same format specification over and over again, e.g. in a for-loop, you can improve performance if you precompile your format string. See the method compileFormatString(String) in FormatInputStream.
Returns:
the number of variables (format flags) read. If -1 is returned, an exception was caught internally and can be investigated with getInternalException() or rethrown with throwInternalException().

getVersion

public static java.lang.String getVersion()

version

public static void version()
Print, on standard output, information about this class.

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
This is the command line version of Java printf.

Usage:

  java com.braju.format.Format [-help|-version|-printf fmtstr [param]*]

Options:
-helpPrint this message.
-printfUsing the next argument (fmtstr) as the format string and one or more optional parameters (param) as values to be formatted by the printf method.
-versionPrint version information.

Example:

  java com.braju.format.Format -printf "Yeah, %d%% %s." 100 Java

prints

  Yeah, 100% Java.

See Also:
www.braju.com

braju.com
Java printf & scanf (beta)

Copyright 1997-2000, Henrik Bengtsson.