• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Command Parsing

 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok,
I've been working on a chatroom server here's my problem:
I've been using a stringTokenizer to parse the incoming data from the clients. This worked well as I used the first token to identify any commands, and then the next token by default was the message. My problem is that most messages are more than one token long. Right now I have it set up like this:
StringTokenizer stk= new StringTokenizer(str, " ");
String cmd = st.nextToken();
String msg= st.nextToken();
My problem is that most messages (msg) are more than one token long. Is there a way that I could assign all the tokens after the first one to the second string msg?
I suppose that I could place all the tokens into a vector and then this would probably work out. Is there an easier way though just using StringTokenizer?
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I solved my problem. This is what I did. I took all the input coming in from a given client, and I created a StringTokenizer to break it up into tokens. I then put all of the tokens into an array. I then copied all the elements of the array into a vector. Now it was easy to access the first element of the vector, which I decided would always be a command. The Tricky part was taken everything beyond the first element and putting it back together so it'd resemble a message. In order to do this, I created an empty string, and then used a for loop to extract everything from the vector and concant it with the empty string. It sounds alot easier than it was, but I was puzzled for awhile. If anyone thinks that what I've done isn't a good idea please let me know.
Thanks,
Sean
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool It is really nice to see someone follow up with their OWN answer . That way we got to learn from your efforts. Thanks.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sean,
You can move the second token and beyond to a single String directly from the StringTokenizer object. You don't have to move it to an array and then to a Vector.
[This message has been edited by Mark Savory (edited March 07, 2001).]
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My $0.02:
- StringBuffer is more efficient than String when doing a number of concatenations.
- I would look at using the indexOf() and substring() methods of String. Would be a lot more straightforward, given your description of the strings you are trying to parse.
J.Lacar
 
Mark Savory
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JUNILU,
We're talking about parsing a delimited String into substrings. Why not reuse StringTokenizer for this? If you think you can improve on the way StringTokenizer does this, maybe you can create a new reusable class that we all can use.
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark,
I think your absolutely right. I did all this late last night, and I knew that it seemed a lot more difficult than it had to be, but once it started working, I didn't take a step back and think about it at all. It's like the quote" Sometimes you have to go a long way to get a short distance."
Anyhow, I think I'll rewrite my code and get rid of that vector and array. Thanks alot for your help.
- Sean
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My point was that given the problem of breaking a string into two: command + message, it just seems a long way around the bush to use a StringTokenizer and break the string into tokens, then rebuild it after extracting the first token. What I would do is this:
<pre>
// given String str = "command This is a message"
String s = str.trim();
int pos = s.indexof(" ");
String command = s.substring(0, pos-1);
String message = s.substring(pos+1);
</pre>
no loops, no concatenation, no new StringTokenizer(), just straightforward string extraction. If it were required to analyze and act on each token in str, then a StringTokenizer() would be appropriate, but with the problem as described, it's overkill IMO.
J.Lacar

Originally posted by Mark Savory:
JUNILU,
We're talking about parsing a delimited String into substrings. Why not reuse StringTokenizer for this? If you think you can improve on the way StringTokenizer does this, maybe you can create a new reusable class that we all can use.



[This message has been edited by JUNILU LACAR (edited March 07, 2001).]
 
Mark Savory
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JUNILU,
I stand corrected. Given the problem statement, your code looks most efficient.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic