开发者

How can I ensure that there's whitespace between specific tokens with StreamTokenizer?

I am working with StreamTokenizer to parse text, and I need to make sure that there's whitespace between certain tokens. For example, "5+5" is illegal, but "5 + 5" is valid.

I really don't know StreamTokenizer that well; I read the API but couldn't f开发者_如何学运维ind anything to help me. How can I do this?


You'll have to set spaces as "ordinary characters". This means that they'll be returned as tokens on their own, but not folded into other tokens. Example:

StreamTokenizer st = new StreamTokenizer(new StringReader("5 + 5"));
st.ordinaryChar(32);
int tt = st.nextToken();  // tt = TT_NUMBER, st.nval = 5
tt = st.nextToken();      // tt = 32 (' ')
tt = st.nextToken();      // tt = 43 ('+')
tt = st.nextToken();      // tt = 32 (' ')
tt = st.nextToken();      // tt = TT_NUMBER, st.nval = 5
tt = st.nextToken();      // tt = TT_EOF

Unfortunately, you'll have to deal with whitespace tokens in your parser. I'd recommend rolling your own tokenizer. Unless you're doing something quick and dirty, StreamTokenizer is almost always the wrong choice.


Consider using Scanner instead since. E.g.:

String input = "5 + 5";
Scanner scanner = new Scanner(input).useDelimiter(" ");
int num1 = scanner.nextInt();
String op = scanner.next();
int num2 = scanner.nextInt();

This is a simplified example that assumes an input like "5+5" or "5 + 5". You should be checking scanner.hasNext() as you go, perhaps in a where loop to process more complicated inputs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜