Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / October 2003

Tip: Looking for answers? Try searching our database.

StringTokenizer to return empty tokens

Thread view: 
VS - 15 Oct 2003 19:13 GMT
I want to use StringTokenizer to convert a string like
dasbndc|716372|3|5|||||abcd
to an array:
array[0] = dasbndc
array[1] = 716372
array[2] = 3
array[3] = 5
array[4] = ""                        <============= empty string !!!
array[5] = ""                        <============= empty string !!!
array[6] = ""                        <============= empty string !!!
array[7] = ""                        <============= empty string !!!
array[8] = abcd

I initialized the tokenizer with
               tk = new StringTokenizer(line,"|");

The following never returns the blank tokens:
               while (tk.hasMoreTokens())
               {
                        System.out.println(tk.nextToken());
                }
Any thoughts/replies welcome
Vincent
Chris Smith - 15 Oct 2003 19:30 GMT
> I want to use StringTokenizer to convert a string like
> dasbndc|716372|3|5|||||abcd
[quoted text clipped - 8 lines]
> array[7] = ""                        <============= empty string !!!
> array[8] = abcd

StringTokenizer doesn't work like that.  There are kludges, but ideally,
you'd use a class that does.  For example, Jon's JlsTokenizer at
http://www.yoda.arachsys.com/java/skeetutil/.

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Paul Lutus - 15 Oct 2003 19:46 GMT
> I want to use StringTokenizer to convert a string like
> dasbndc|716372|3|5|||||abcd
[quoted text clipped - 8 lines]
> array[7] = ""                        <============= empty string !!!
> array[8] = abcd

Since StringTokenizxer is known not to be able to handle this problem, and
IMHO is really dreadful to use, why not do this instead:

String source = "dasbndc|716372|3|5|||||abcd|||";

String[] array = source.split("\\|",-1);

The "-1" argument forces processing of trailing blank fields. Note that I
included some in the data for this test.

Like this:

**************************************************

public class Test {
 
  public static void main(String[] args)
  {
     String source = "dasbndc|716372|3|5|||||abcd|||";
     
     String[] array = source.split("\\|",-1);

     for(int i = 0;i < array.length;i++) {
        System.out.println("[" + array[i] + "]");
     }
  }
}

**************************************************

Result:

[dasbndc]
[716372]
[3]
[5]
[]
[]
[]
[]
[abcd]
[]
[]
[]

Signature

Paul Lutus
http://www.arachnoid.com

TresWBL - 16 Oct 2003 18:58 GMT
> String source = "dasbndc|716372|3|5|||||abcd|||";
>
> String[] array = source.split("\\|",-1);
>
> The "-1" argument forces processing of trailing blank fields. Note
> that I included some in the data for this test.

I'm wondering, how did you know that the split method could take a second
argument? I looked here ( http://java.sun.com/j2se/1.4.2/docs/api/ ) and it
only showed split having one argument. Thanks.

Signature

TresWBL
telconstar99@NOSPAMhotmail.com

Paul Lutus - 16 Oct 2003 21:00 GMT
>> String source = "dasbndc|716372|3|5|||||abcd|||";
>>
[quoted text clipped - 6 lines]
> argument? I looked here ( http://java.sun.com/j2se/1.4.2/docs/api/ ) and
> it only showed split having one argument.

Yes, that's true, but only if you don't actually read it completely.

> Thanks.

Okay, all right. I confess. I cheated. I apologize. I ... I ... (gasp,
choke) ... read the documentation! (breaks down and weeps uncontrollably).

Here is an excerpt from the same document set you visited. Put it on one
line:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
 #split(java.lang.String,%20int)

Signature

Paul Lutus
http://www.arachnoid.com

Tor Iver Wilhelmsen - 15 Oct 2003 20:34 GMT
>                 tk = new StringTokenizer(line,"|");

Try a third parameter to the constructor - check the docs.

> Any thoughts/replies welcome

You need to return the delimiters as tokens in order to have
StringTokenizer not skip multiple delimiters.

But if you can use 1.4.x APIs, do as others have suggested and use
String.split().
RC - 15 Oct 2003 21:01 GMT
> I want to use StringTokenizer to convert a string like
> dasbndc|716372|3|5|||||abcd
[quoted text clipped - 17 lines]
>                          System.out.println(tk.nextToken());
>                  }

    String tmpstr = "";
    while (tk.hasMoreTokens()) {
        tmpstr = tk.nextToken();
        if (tmpstr == null)
            tmpstr = "";
        System.out.println(tmpstr);
    }

Much better

String[] array = line.split("|");
Stephen Ostermiller - 21 Oct 2003 23:53 GMT
Use the com.Ostermiller.util.StringTokenizer which has a method that
you can set to have it return empty tokens.

http://ostermiller.org/utils/StringTokenizer.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.