• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on StringTokenizer  RSS feed

 
thejaswini ramesh
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I have a question regarding StringTokenizer.Is there no way to verify whether a string entered by a user is colon seperated or not(Im talking abt TL1 commands which have a format like ths "cmd:tid:aid:ctag;".
If i have to check whether string entered is in ths format,i can check the semicolon with string.endsWith()...but when i do:

StringTokenizer st = new StringTokenizer(str, ":");
int n = st.countTokens();

n shows a value of 1 even when i enter a completely invalid string like "abcdef"

Am a bit confused...am i using the wrong class to check the presence of ":"in my string??Please help...

Thanks,
Thejaswini.
[ January 09, 2006: Message edited by: thejaswini ramesh ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to check if a string matches a certain format, you could use regular expressions. Lookup the API documentation of the package java.util.regex - it contains information on how to use regular expressions in Java.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

StringTokenizer st = new StringTokenizer(str, ":");
int n = st.countTokens();

n shows a value of 1 even when i enter a completely invalid string like "abcdef"

This is the correct behavior. It tried to break up the string "abcdef" into parts separated by ":". Since there were no colons in the string, it broke it into one part. So a count of one means no delimiters were found.

If you try it on "ab:cd:ef" it should say tokenCount is 3. If you need to keep track of "empty" tokens like the middle one in "ab::ef" use the optional boolean parameter on the constructor to keep the delimiters. That will show tokenCount of 4 because it's counting the colons, too.

Any of that help?
 
Adam Richards
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help spread the word: The word "doubt" is inappropriate and confusing for subject lines. I have no idea what you mean by "doubt." Did you mean "confusion"? Even "confusion," though, is too vague. The more meaningful & specific your subject, the more meaningful & specific the replies will be.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adam Richards:
Please help spread the word: The word "doubt" is inappropriate and confusing for subject lines. I have no idea what you mean by "doubt." Did you mean "confusion"? Even "confusion," though, is too vague. The more meaningful & specific your subject, the more meaningful & specific the replies will be.


That's a bit harsh. Using the noun "doubt" for "question" is a well-known idiom in the Indian subcontinent. So relax, smoke a beedi, and all this will become clear
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by thejaswini ramesh:
.Is there no way to verify whether a string entered by a user is colon seperated or not...


There is a whole branch of mathematics devoted to understanding language and verifying if a string is formatted in a meaningful way. In fact, this theory is used to create compilers and interpreters. Although, I doubt you need to build a full-blown lexer and parser, you may find regular expressions helpful to verify that the input is in the format you expect. StringTokenizer will work, too, as long as you understand its limitations.

Layne
 
thejaswini ramesh
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for your time.Stan like you suggested using the boolean parameter in the constructor seems to be working to keep track of the empty tokens(as there are optional parameters in the TL Command string which are not used at times).Thank you.
Thanks Jesper and Layne...am trying the same thing with regular expressions.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!