When using a string tokenizer , how do I check the next character after a token if it is a delimiter?
If you use the 3 argument constructor for StringTokenizer
, then 3rd argument says whether or not the delimiters are returned as tokens.
However1, this still leaves you with the awkward problem of dealing with whitespace. For example "1+1" gives you 3 tokens, but "1 + 1" gives you 5 tokens. You either need to:
- pick a better way2 to tokenize the input,
- filter out any white-space "tokens" before you start analyzing them, or
- remove any whitespace from the input string before you tokenize it.
1 - I'm inclined to agree with @EJP that StringTokenizer
is the wrong tool for this job. You could make it work, but IMO you shouldn't. (Unless you have been explicitly directed to do this.)
2 - Better ways might be StreamTokenizer
, String.split
(with a cunning regex involving lookaheads or lookbehinds), a tokenizer generated using ANTLR, javacc or some other parser generator, or ... a hand built tokenizer.