java - Streamtokenizer to read very large numbers? -
i have take input containing large numbers of order 10^9 in java. how handle inputs fast? since streamtokenizer.nval gives double, how can read larger values??
before parsing, reset tokenizer syntax table , initialize recognize numbers words:
streamtokenizer tokenizer = new streamtokenizer(r); tokenizer.resetsyntax(); tokenizer.whitespacechars(0, 32); tokenizer.wordchars('0', '9'); tokenizer.wordchars('-', '.'); tokenizer.wordchars('+', '+'); tokenizer.wordchars('a', 'z'); tokenizer.wordchars('a', 'z'); tokenizer.wordchars(0xa0, 0xff); // not needed here. */ tokenizer.slashslashcomments(true); tokenizer.slashstarcomments(true); tokenizer.quotechar('"'); tokenizer.quotechar('\'');
then, when encountering word, check whether parseable number (a bit crude here, shows general idea):
... case streamtokenizer.tt_word: if ("true".equals(tokenizer.sval)) { result = boolean.true; } else if ("false".equals(tokenizer.sval)) { result = boolean.false; } else if ("null".equals(tokenizer.sval)) { result = null; } else { try { result = long.parselong(tokenizer.sval); } catch(numberformatexception e) { try { result = double.parsedouble(tokenizer.sval); } catch (numberformatexception e2) { throw new illegalstateexception( "unexpected token: " + tokenizer.tostring()); } } } tokenizer.nexttoken(); break;
whether works depends on use case: if want parse expressions (and not json in case), don't want set +
or -
word characters, general idea should still work treating them unary operators , detecting constants @ later stage.
Comments
Post a Comment