javaparsing

java.io.StreamTokenizer.TT_NUMBER: Floating or Integer?


is there a way to find if the value parsed and returned by java.io.StreamTokenizer.nval (e.g. 200) was an integer or a floating number ?

Thanks

Edited:

I want to be able to know if the input was '200' or '200.0'.


Solution

  • I had the same question, but I couldn't find any good solutions online.

    I figured the solution by looking at the jdk source:

    1. StreamTokenizer's constructor calls, amongst other methods, parseNumbers

    2. Considering that there is no way to prevent the default constructor from being invoked, you can only disable number parsing after the fact. Use the resetSyntax method.

    3. Finally copy the methods invoked by the default constructor and set numbers and points to be normal characters.

    The result looks like this:

        var tokenizer = new StreamTokenizer(...);
        tokenizer.wordChars('a', 'z');
        tokenizer.wordChars('A', 'Z');
        tokenizer.wordChars(128 + 32, 255);
        tokenizer.whitespaceChars(0, ' ');
        tokenizer.commentChar('/');
        tokenizer.quoteChar('"');
        tokenizer.quoteChar('\'');
        for (int i = '0'; i <= '9'; i++) {
            tokenizer.wordChars(i, i);
        }
        tokenizer.wordChars('.', '.');
        tokenizer.wordChars('-', '-');
    

    Now you can access the numbers as a TT_WORD and do your own parsing to distinguish between floating points and integers. It took 15 years but I hope someone will find this answer