You can't do this with StreamTokenizer. When I pointed this out to the JDK
people some time ago, they told me that I could subclass StreamTokenizer to add
this functionality. Unfortunately, you *can't* because of the way the members
and some of the methods are specified.
What I finally did was write my own. It's available in the NCSA Portfolio Java
3D library that we distribute at:
http://havefun.ncsa.uiuc.edu/Java3D/portfolio/
Look for the "ncsa.util.ReaderTokenizer" class.
I just whipped up a program that uses ReaderTokenizer and your getNextFloat()
method. Attached is a copy
of the program and its output. Everything worked fine. (the last line is
"ParseRdr error" because it reached
"end of file").
Steve
"Kearney, Ken" wrote:
> Sorry if this is out of line for java-3d but could someone provide me with a
> code snippet or info on how to reading scientific notation numbers ( e.g.
> 5.03E-9 ) from a file?? The file I'm reading contains floats and scientific
> notation numbers. I'm using StreamTokenizer is this my problem?? I get a
> message that "StreamTokenizer" is deprecated but I'm not sure what to use
> instead.
>
> My code flows as follows::
> ~~~~~~~~~~~~~~
> protected StreamTokenizer st;
> protected float getNextFloat(){
> try {
> st.parseNumbers();
> st.nextToken();
> System.out.println("in getNextFloat: "+st.toString());
> if(st.ttype == st.TT_NUMBER){
> return((float) st.nval);
> } else {
> System.out.println("ParseRdr error, expected a float");
> return(-1f);
> }
> } catch (Exception e) {
> System.out.println("ParseRdr " + e);
> }
> return(-2f);
> }
> ~~~~~~~~~~~~~~
> the file I'm reading from flows like this::
> ~~~~~~~~~~~~~~~
> 3.000000
> 30000.00
> 1.6200000E-02
> 0.7000000
> 175.0000
> ~~~~~~~~~~~~~~~
> when "st" gets to the "E" in "1.6200000E-02" it is treated like the
> nextToken
>
> I'll need to write them back out to a file also but that can probably be
> easily figured out by the student given an example on reading them.
>
> Thanx
>
> Kenneth Kearney
> 516.346.6014, [EMAIL PROTECTED]
> Northrop Grumman Corporation
> =====================================================================
> To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
> Java 3D Home Page: http://java.sun.com/products/java-media/3D/
--
Steve Pietrowicz - [EMAIL PROTECTED] Project Manager - NCSA Java 3D Group
NCSA Portfolio: http://havefun.ncsa.uiuc.edu/Java3D/portfolio/
New Beta 2a release! New Loaders, record and replay of your
Java 3D apps and more! Freely available for non-commercial use!
You Build It VR: http://havefun.ncsa.uiuc.edu/Java3D/YouBuildItVR/
Build your own multi-user virtual worlds with no programming experience!
The Java3D FAQ: http://tintoy.ncsa.uiuc.edu/~srp/java3d/faq.html
Java News Network: http://tintoy.ncsa.uiuc.edu/~srp/java/javanews.html
public class foo {
protected ReaderTokenizer st;
public static void main(String[] argv) {
foo f = new foo();
}
public foo() {
try {
File f = new File("test.dat");
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis, "ISO8859_1");
st = new ReaderTokenizer(isr);
for (;;) {
float fl = getNextFloat();
if (fl == -1)
System.exit(0);
System.out.println("getNextFloat = "+fl);
}
} catch (Exception e) {
System.out.println("barf");
}
}
protected float getNextFloat(){
try {
st.parseNumbers();
st.nextToken();
System.out.println("in getNextFloat: "+st.toString());
if(st.ttype == st.TT_NUMBER){
return((float) st.nval);
} else {
System.out.println("ParseRdr error, expected a float");
return(-1f);
}
} catch (Exception e) {
System.out.println("ParseRdr " + e);
}
return(-2f);
}
}
$ java foo
in getNextFloat: ncsa.util.ReaderTokenizer@162b4c88
getNextFloat = 3.0
in getNextFloat: ncsa.util.ReaderTokenizer@162b4c88
getNextFloat = 30000.0
in getNextFloat: ncsa.util.ReaderTokenizer@162b4c88
getNextFloat = 0.0162
in getNextFloat: ncsa.util.ReaderTokenizer@162b4c88
getNextFloat = 0.7
in getNextFloat: ncsa.util.ReaderTokenizer@162b4c88
getNextFloat = 175.0
in getNextFloat: ncsa.util.ReaderTokenizer@162b4c88
ParseRdr error, expected a float