File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes catching null in a StringTokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "catching null in a StringTokenizer " Watch "catching null in a StringTokenizer " New topic

catching null in a StringTokenizer

swatione chowdary

Joined: Apr 07, 2005
Posts: 27
can any one give me the solution on this

i have a situation like this ,

i have a String like this


where '~' is a delimeter.

i passed this string in to a StringTokenizer

in a StringTokenizer object if two delimeters are adjacent i have to consider it as null token.

as the methods( nextToken() , nextElement() ) in StringTokenizer are not returning null.

how can i achieve it even through programmetically.

can any one give this logic.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

Maybe it's giving you an empty string instead of a null object-reference variable.

Compare the results against the empty String: ""

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Look at the "other" constructor on StringTokenizer. You can tell it to return the delimiters and the data. It can be a bit tricky to use the results. Show us what you make!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
swatione chowdary

Joined: Apr 07, 2005
Posts: 27
hear is the code for this problem, you can ask questions where you cant understand.

i request every one to post the solutions like this.

the code which is in the while is the actual code for the question raised.

i invite your comments.

i think that it is not possible to do with the two previous replyes.

[edit - added code tags and broke long line]
[ June 27, 2007: Message edited by: David O'Meara ]
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
ok,I dont know if i am clear with your "actual doubt" but,
below shown is the link i hope it can help you to solve your problem .
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

I'm not sure all of the code is required, a comment like "check the API for StringTokenizer will help, specifically the constructor that accepts a boolean".

Viola, solved
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
As far as I know even the modern String.split does the trick (giving empty strings instead of null's).
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
This test code shows the difference between a StringTokenizer object and the String split() method.

I think in your case you want to split() the input String.
I agree. Here's the link:
subject: catching null in a StringTokenizer
It's not a secret anymore!