Corrections to Chapter 16 of Java I/O, Formatted I/O with java.text

p. 470: In the first line of code on the page

public final static NumberFormat getpercentInstance()

should be

public final static NumberFormat getPercentInstance()

On p. 497, the parse() method of Example 16-9, ExponentialFormat, doesn't properly handle all possible exponents. Here's a corrected version:

  public Number parse(String text, ParsePosition parsePosition) {
  
    int oldIndex = parsePosition.getIndex();

    try {
      double result = parser.parse(text, parsePosition).doubleValue();
      int eposition = text.toUpperCase().indexOf('E');
      if (eposition != -1) {
        // advance past the E
        parsePosition.setIndex(eposition + 1);
        // ignore a + sign
        if (text.charAt(parsePosition.getIndex()) == '+') {
          parsePosition.setIndex(parsePosition.getIndex() + 1);
        }
        int exponent = parser.parse(text, parsePosition).intValue();
        result *= Math.pow(10, exponent);
      }
      return new Double(result);
    }
    catch (Exception e) {
      parsePosition.setIndex(oldIndex);
      return null;
    }
    
  }

[ Java I/O Corrections | Java I/O Home Page | Table of Contents | Examples | Order from Amazon ] ]

Copyright 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified July 15, 1999