Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

String parsing question

Thread view: 
Colin - 21 Mar 2007 19:08 GMT
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
Christian - 21 Mar 2007 20:25 GMT
Colin schrieb:
> Hello,
>
[quoted text clipped - 18 lines]
> 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
Oliver Wong - 21 Mar 2007 20:36 GMT
> Hello,
>
[quoted text clipped - 13 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.