• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Alternative to StringTokenizer

 
Bhushan Shete
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any alternative ( other than substring ) that can be used instead of StringTokenizer ???
Actually, I am facing a problem with StringTokenizer. If I have a string with a character '=' in it, then the following code results in false tokens :
StringTokenizer valToken = new StringTokenizer(val,"==>");
while(valToken.hasMoreTokens())
{
valno = valToken.nextToken();
valdate= valToken.nextToken();
}
 
Dave Turner
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you only need to include a character once in the delim parameter of the StringTokenizer constructor(eg. StringTokenizer(val,"=>"). What does the String you are trying to tokenize contain?
 
Laojar Chuger
Ranch Hand
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the exact delimitor? If "=" is your delimitor, only "=" should be put inside the constructor.
 
Bhushan Shete
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The delimiter is exactly the same as I have shown in my query / code. Actually, I have a string as follows :
abcdtest==>xyztest
Where, abcdtest is the value of one variable & xyztest, value of another variable. I concatenate these two variables along with the delimiter.
Now, if my variable contains abcd=test, then it becomes :
abcd=test==>xyztest
And here is where I get false tokens.
I am still trying to sort out this problem.
Anyways, Thanx for ur replies.
 
eric moon
Ranch Hand
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's important to realize that StringTokenizer always delimits using a SINGLE character. The tokenizing string you send it is actually a LIST of characters that will function as tokens. If you want to use a multicharacter delimiter, such as "==>", string tokenizer will not work for you. So, you could do what I did, which is look at the java source for StringTokenizer, and design you own class that does what you want. It's actually much simpler than designing a multi-character tokenizer. I'd post my code, but I wrote it for work, and I'm under non-disclosure. It's not that hard, tho. Just implement the enumerator interface methods.
HTH
e
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See my reply here.
[This message has been edited by Jim Yingst (edited March 20, 2001).]
 
Peter Tran
Bartender
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhushan,
Read this article on Pitfall 2 to see how you can implement a work-around to this problem.
-Peter
 
Bhushan Shete
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a lot to everyone. I have gone thru the source code (actual implementation of StringTokenizer provided by SUN.)
I am thru with it now.
Thanx a lot once again.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic