File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Alternative to StringTokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Alternative to StringTokenizer " Watch "Alternative to StringTokenizer " New topic

Alternative to StringTokenizer

Bhushan Shete

Joined: Mar 20, 2001
Posts: 9
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,"==>");
valno = valToken.nextToken();
valdate= valToken.nextToken();
Dave Turner
Ranch Hand

Joined: Mar 13, 2001
Posts: 60
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

Joined: Dec 20, 2000
Posts: 111
What's the exact delimitor? If "=" is your delimitor, only "=" should be put inside the constructor.
Bhushan Shete

Joined: Mar 20, 2001
Posts: 9
The delimiter is exactly the same as I have shown in my query / code. Actually, I have a string as follows :
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 :
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

Joined: Nov 26, 2000
Posts: 133
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.

<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>"Those who cast the votes decide nothing. Those who count the<BR>votes decide<BR>everything." <BR> -Joseph Stalin<HR></BLOCKQUOTE>
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
See my reply here.
[This message has been edited by Jim Yingst (edited March 20, 2001).]

"I'm not back." - Bill Harding, Twister
Peter Tran

Joined: Jan 02, 2001
Posts: 783
Read this article on Pitfall 2 to see how you can implement a work-around to this problem.
Bhushan Shete

Joined: Mar 20, 2001
Posts: 9
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.
I agree. Here's the link:
subject: Alternative to StringTokenizer
It's not a secret anymore!