Re: String parsing question
"Colin" <razael1@gmail.com> wrote in message
news:1174500491.531117.181800@e65g2000hsc.googlegroups.com...
Hello,
I'm working on a Java program that supports the GTP protocol (Protocol
for programs playing the Go board game). GTP supports commands of the
following format:
[id] command_name [args]
where id is an integer, command_name is a string, args is a space-
separated list of strings, and elements in [] are optional.
I'm using StringTokenizer, and I'm wondering about the best way to
determine if the first element is an integer (id has been given) or a
string (it was omitted). I can think of a few ways to do this, but
none of them seem very elegant. For example, I could extract the
first character of the token and see if it is a digit. What happens
if I use Integer.parseInt(token) and token is a string? Is an
exception thrown?
Yes.
What do you think is the best way to handle this?
If that *REALLY* is the *ENTIRE* GTP protocol, then I'd just try to
parse it via Integer.parseInt(token), catch the exception, and deal with
it. People will tell you don't use exceptions for control flow, but I'd
rather not spend a lot of effort dealing with such a small part of the
application. I'm too lazy to iterate through the token with
Character.isDigit(), and anyway, that won't guarantee the number is
parseable anyway (consider the 5-character string "123IV5" where 'IV' is a
single character representing the roman numeral 4; every character is a
digit, but this is not parseable as a number). If someone knows of a
standard Sun API along the lines of Integer.isNumber(String s), let me
know.
If the GTP protocol actually gets more complicated than that, I'd
build a "real" parser. Depending on the complexity of the protocol, either
a hand-built recursive descent parser (read any compiler textbook for
tutorials on this), or use a parser-generator like ANTLR, where I'd write
out the grammar of the protocol and have ANTLR produce the parser for me.
- Oliver