Hello,
        I'm using the JDK 1.1.5, of course the Linux version.
I have to parse a file of many lines, each line has 1 float and 4 double
numbers.
I read the lines and for each line I parse the 5 numbers and store them in
the variables of a Body object. The line is read into the String bodyinfo.

To parse di string I'm using this method:

java.text.DecimalFormat.parse(String, ParsePosition)


(Please notice that I'm still quite new with Java, so if this isn't a smart
way to parse these lines, please let me know.)

Anyway here is the code fragment:


ParsePosition p = new ParsePosition(0); // Posizione
DecimalFormat df = new DecimalFormat(); // Parser

body[i].base = df.parse(bodyinfo, p).floatValue();
body[i].posx = df.parse(bodyinfo, p).doubleValue();
body[i].posy = df.parse(bodyinfo, p).doubleValue();
body[i].velx = df.parse(bodyinfo, p).doubleValue();
body[i].vely = df.parse(bodyinfo, p).doubleValue();


The official 1.1.5 docs say:


When parsing, leading whitespace is discarded (with successful parse), while 
trailing whitespace is left as is. 

         Example: Parsing "_12_xy" (where _ represents a space) for a number, 
         with index == 0 will result in the number 12, with status.index 
         updated to 3 (just before the second space). Parsing a second time 
         will result in a ParseException since "xy" is not a number, and leave 
         index at 3.

The string "23.456 12 23.6 0 0.1" (no withspace at the beginning) gives:

Base: 23.456
PosX: 0.0
PosY: 0.0
VelX: 0.0
VelY: 0.0

The 0.0 values are perhaps null values (see below).

The ParsePosition is 0 before the first parse and stays at 6 (points the
whitespace after the 23.456) after it.

I guessed that the behaviour of this method is different from what is 
written in the docs: the method does not discard the whitespaces.

The proof is the following: I put a withspace at the beginning of the string:
" 23.456 12 23.6 0 0.1".

And the result was:

Base: 0.0
PosX: 0.0
PosY: 0.0
VelX: 0.0
VelY: 0.0

Again, these could be null values (see below).


Before submitting the bug to this list I chose to run a test on a Solaris
JDK 1.2 of my Computer Science Department (i.e. I haven't installed and
configured it).

For this test I used the string without the whitespace at the beginning.
I compiled the code with the Solaris javac compiler (should have I tried also
with the Linux-compiled java bytecode?) and ran it.
I got a NullPointerException in the second parse call, surely because the
doubleValue() was called on the return value (null) of the first parse.

Needless to say, when I tried with the string with a whitespace at the 
beginning, I got the exception in the first parse.


This seem to say that:

1) This parse method has a behaviour different from the one described in
the docs and seems to be there since the solaris code (I guess that the
Linux JDK is derived from the solaris code).

2) There is a different buggy behaviour: in Solaris the method returns a
null, in Linux seems that it returns a zero value...or the doubleValue
returns 0 if called on a null pointer. In the latter case, this does not
happen in Solaris.


I hope this helps to fix the bug. If you need more informations just ask.


BTW, I wrote a workaround. I just increase the ParsePosition p variable...

p.setIndex(p.getIndex() + 1);

.every time after a parse call.

With this trick, all works perfectly:

Base: 23.456
PosX: 12.0
PosY: 23.6
VelX: 0.0
VelY: 0.1

This is the final proof that the problem are the whitespaces.

Best regards,
        Andrea


---
Andrea "Kontorotsui" Controzzi - MALE Student of Computer Science at 
University of Pisa  -  Italy  -  E-mail: [EMAIL PROTECTED]
My home page: http://www.cli.di.unipi.it/~controzz/intro.html

Founder and Admiral of Hoshi no Senshi (italian Leiji Matsumoto's fan group).
Creator of It.Arti.Cartoni (italian anime newsgroup) and proud member of...

+-------------------------------------------------------------+
|                                    .                  * .   |
|           .        __         .             .               |
|                oq  |    po               _ _                |
|              /  #==>>>==#            ,-' (_)\               |
|              |  ,-|~\\       ///_ ,()  ,_____}              |
|              |  |/|~]]] /// ,-~'  .,~   /   \|      .       |
|              |\_|_|_\_\~~----~~'   \   (    /|    .         |
| .            /~ \___/           [m] \   \__//               |
|             _bo..__             //   `-,.~~                 |
|          _-~ 0000000000000ooooo.__    (         .           |
|         \      000000000000000000000000o  .                 |
|      .          (_)0000000000000000000000                   |
|    .         \~~~*,,,* ~000000000000000000                  |
|                            ~0000000000000             .     |
|                               ~~~---~~                      |
|                   .*                                        |
+-------------------------------------------------------------+
|     An e-mail network of Space Cruiser Yamato and           | 
|                      StarBlazers Fans                       |
+-------------------------------------------------------------+

Reply via email to