JDK-4079180 : java.io.StreamTokenizer: Add ability to read full Java floating-point syntax
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.1.3,1.1.4,1.1.7,1.2.0,1.2.2
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS:
    generic,solaris_2.5.1,windows_95,windows_nt generic,solaris_2.5.1,windows_95,windows_nt
  • CPU: generic,x86,sparc
  • Submitted: 1997-09-15
  • Updated: 2002-02-15
  • Resolved: 2002-02-15
Related Reports
Duplicate :  
Description

Name: tb29552			Date: 09/15/97


java.io.StreamTokenizer can't read valid floating point
numbers such as:

    -6.77e-005

Strings of this type can be read are valid in
Java.   Double.valueOf("-6.77e-005") give the
value I expect when it is printed out.  StreamTokenizer
doesn't handle this.

The following java program illustrates this problem:

-------

import java.io.*;
 
public class test2 {
 
        public static void main(String argv[]) {
                try {
                Reader r = new BufferedReader(new FileReader("data"));
                StreamTokenizer st = new StreamTokenizer(r);
 
                st.parseNumbers();
 
                st.nextToken();
                while (st.ttype != StreamTokenizer.TT_EOF) {
                        if (st.ttype == StreamTokenizer.TT_NUMBER)
                                System.out.println(st.nval);
                        else if (st.ttype == StreamTokenizer.TT_WORD) {
                                System.out.println("error!  got word!");
                                System.out.println("saw "+st.sval+" on line "+st
.lineno());
                        } else
                                System.out.println("error!");
                        st.nextToken();
                }
                } catch (Exception e) {
                        System.out.println(e);
                }
 
        }
}

-------
The following data file can be used with the program
above:
-------cut here - only include the numbers not this line
1
2.2
3.33
0.44
-6.77e-005
-------cut here - only include the numbers not this line


company - National Center for Supercomputing Applications , email - ###@###.###
======================================================================

Name: skT45625			Date: 03/08/2000


java version "1.2.2"
Classic VM (build JDK-1.2.2-W, native threads, symcjit)


StreamTokenizer currently extracts numbers expressed in scientific notation as a
number plus a word.  For example, the string "1.23E4" is parsed as number 1.23
and word "E4". This request is to add a method (sample name
StreamTokenizer.parseFloats( boolean flag)), that would allow the user to
specify the behavior of StreamTokenizer (similar to the slashStartComments() and
slashSlashComments() methods).  When flag = true, 1.23E4 would be extracted as a
single number, when flag = false 1.23E4 would be extracted as a number plus a
word, as is current.  To maintain compatibility with current releases this
setting should default to false.
(Review ID: 102224)
======================================================================

Comments
WORK AROUND Name: skT45625 Date: 03/08/2000 Manually check all words directly following numbers returned by StreamTokenizer to see if they consist of an "E" followed by an integer. (Review ID: 102224) ======================================================================
11-06-2004

EVALUATION Though I like the intent in the supplied fix (and completely agree that all 4 enhancements are desirable), I have the following reservations: - I would use Double.valueOf() instead of doing the calculation explicitly. - Makes me nervous that the diffs have debug statements still lingering. - Even if someone has code for the TT_INTEGER/TT_DOUBLE change, (which should be TT_LONG, btw), we should do it in a backwards compatible way (or kgh@eng would scream bloody murder) -- need to think about this some more. So for now, I am not checking this change in until someone gets a chance to think through these things. anand.palaniswamy@Eng 1997-10-15 According to JLS, the way numbers are parsed would not recognize floating point numbers. Made this a RFE. ###@###.### 1998-03-13 To clarify: The present specification says that number parsing stops before a non-initial '-', the second occurrence of '.' or the first non-numeric character encountered. To remain compatible with the existing spec, the cleanest way to add the requested functionality would be to add a new method public void parseExponentialNumbers(); that would have the same effect as parseNumbers() but would also enable the parsing of exponents. -- mr@eng 3/13/1998 Due to compatibility restraints we will not further evolve this legacy class. ###@###.### 2002-02-14
13-03-1998