Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Hubert Klein Ikkink (aka Mr.Haki) - Communications Officer
 Mr.Haki's JBuilder Jar #20
See Also: JBuilder Papers

Formatting booleans
When working with boolean data types we must use the values true and false. But sometimes we want to use different values for a boolean data type. For example we want to use the strings "yes" and "no" to set the value of a boolean variable.

We can achieve this by using the borland.jbcl.util.BooleanFormat class. This class contains several methods to parse strings to boolean values and change boolean values to string values. BooleanFormat is extended from the java.text.Format class.

To work with this class we must create an object instance and assign a pattern to this object. This pattern defines the string representations of the boolean values true, false or unknown. The values are seperated by a semi-colon (;). An example of pattern which will assign the string "yes" to true, "no" to false and "(unknown)" to a boolean null value would be:

  "yes;no;(unknown)"
Once we have a BooleanFormat object available, with a pattern, we can use the format() methods to transform string values to boolean values. One of the arguments of the format() method is of class type java.text.FieldPosition. Usually a Format contains constants for this argument, but the BooleanFormat class doesn't, so we must create a dummy FieldPosition object. To convert a boolean value to the string representation we can use the parse() methods. This method takes as one of it's arguments a java.text.ParsePosition object. Just as with the FieldPosition we must create a dummy object when using the parse() method.

Look at the help for this class because it contains some nice examples on how to use the class.

Example

  import borland.jbcl.util.BooleanFormat;

  public class BooleanFormatExample {
    public static void main(String[] args) {
      /*
       * Create format object
       */
      BooleanFormat bf = new BooleanFormat();

      /*
       * Create pattern and apply to format object
       */
      String pattern = "yes;no;(unknown)";
      bf.applyPattern(pattern);

      /*
       * Create StringBuffer object to store converted result in
       */
      StringBuffer convertResult = new StringBuffer();

      /*
       * Do the actual conversion and show result to console.
       * First for null value.
       * Output: convertResult? (unknown)
       */
      bf.format(null, convertResult, new java.text.FieldPosition(0));
      System.out.println("convertResult? " + convertResult.toString());
      /*
       * Then for true value
       * Output: convertResult? yes
       */
      convertResult = new StringBuffer();
      bf.format(new Boolean(true), convertResult, new java.text.FieldPosition(0));
      System.out.println("convertResult? " + convertResult.toString());
      /*
       * And finally false value
       * Output: convertResult? no
       */
      convertResult = new StringBuffer();
      bf.format(new Boolean(false), convertResult, new java.text.FieldPosition(0));
      System.out.println("convertResult? " + convertResult.toString());

      /*
       * And now the other way around: string -> boolean.
       * First for String value yes
       * Output: convertBoolean? true
       */
      Boolean convertBoolean = bf.parse("yes", new java.text.ParsePosition(0));
      System.out.println("convertBoolean? " + convertBoolean.booleanValue());
      /*
       * Then for String value no
       * Output: convertBoolean? false
       */
      convertBoolean = bf.parse("no", new java.text.ParsePosition(0));
      System.out.println("convertBoolean? " + convertBoolean.booleanValue());
      /*
       * We don't need the complete string as long as
       * it is uniquely identifyable.
       * Output: convertBoolean? true
       */
      convertBoolean = bf.parse("y", new java.text.ParsePosition(0));
      System.out.println("convertBoolean? " + convertBoolean.booleanValue());
    }
  }

When we run this application we get the following output:

  convertResult? (unknown)
  convertResult? yes
  convertResult? no
  convertBoolean? true
  convertBoolean? false
  convertBoolean? true


This webpage © 1997-2009 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved.