String parsing question

C

Colin

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?

What do you think is the best way to handle this?

Thanks,
Colin
 
C

Christian

Colin said:
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?

What do you think is the best way to handle this?

Thanks,
Colin

I barely use the string tokenizer.
I would just use java's regexp package to separate and verify your
commands in one step.

Christian
 
O

Oliver Wong

Colin said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top