本文介绍了我如何从 StreamTokenizer 回避 TT_NUMBER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 StreamTokenizer 用循环替换了对 String.split() 的调用,因为我的用户需要引用功能.但是现在我遇到了将数字转换为数字而不是保留为字符串的问题.在我的循环中,如果我得到 TT_NUMBER,我会使用 Double.toString() 将其转换回字符串.但这对一些必须发送给我的长序列号不起作用;它们正在转换为指数符号.

我可以改变将数字转换回字符串的方式,但我真的不想首先解析数字.我看到 StreamTokenizer 有一个 parseNumbers() 方法,它打开数字解析,但似乎没有办法关闭它.我需要做什么才能创建和配置一个与默认值相同但不解析数字的解析器?

解决方案

您可以通过调用 resetSyntax() 然后重新定义单词来从 StreamTokenizer 重置令牌定义字符也包括数字.

以下内容:

Reader r = new BufferedReader(new StringReader("123 foo \"string \\\"literal\" 0.5"));StreamTokenizer st = new StreamTokenizer(r);st.resetSyntax();st.wordChars(0x23, 0xFF);st.whitespaceChars(0x00, 0x20);st.quoteChar('"');while(st.nextToken() != StreamTokenizer.TT_EOF) {System.out.println(st.sval);}

会打印:

123富字符串字面量0.5

I replaced a call to String.split() with a loop using StreamTokenizer, because my users needed quoting functionality. But now I'm having problems from numbers being converted to numbers instead of left as Strings. In my loop if I get a TT_NUMBER, I convert it back to a String with Double.toString(). But that is not working for some long serial numbers that have to get sent to me; they are being converted to exponential notation.

I can change the way I am converting numbers back into Strings, but I would really like to just not parse numbers in the first place. I see that StreamTokenizer has a parseNumbers() method, that turns on number parsing, but there doesn't seem to be a way to turn it off. What do I have to do to create and configure a parser that is identical to the default but does not parse numbers?

解决方案

You could reset the token definitions from the StreamTokenizer by invoking resetSyntax() and then redefining the word chars to include digits as well.

The following:

Reader r = new BufferedReader(new StringReader("123 foo \"string \\\" literal\" 0.5"));

StreamTokenizer st = new StreamTokenizer(r);

st.resetSyntax();
st.wordChars(0x23, 0xFF);
st.whitespaceChars(0x00, 0x20);
st.quoteChar('"');

while(st.nextToken() != StreamTokenizer.TT_EOF) {
  System.out.println(st.sval);
}

would print:

123
foo
string " literal
0.5

这篇关于我如何从 StreamTokenizer 回避 TT_NUMBER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 06:00