a problem with tokenizer


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: a problem with tokenizer

  1. #1
    Join Date
    Oct 2003
    Posts
    12

    a problem with tokenizer

    Dear friends,

    the string tokenizer works with me well in the begining but how can i set the tokenizer to start processing another part if the code. see the code below :


    String string = "Mike can not drive ";
    .

    System.out.println("\nthe token is :\n") ;

    StringTokenizer stok = new StringTokenizer(string);
    while (stok.hasMoreTokens())
    {
    System.out.println(stok.nextToken());

    }


    queue qu = new queue(stok.nextToken().length());




    The problem is just with the last code. it seems that no tokens has been read. therfore, is there any method that read the tokens from the begining.


    thanks

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    You shouldn't really be surprised that there are no tokens left - you've just exited a loop who's exit condition was that there must be no tokens left!

    As I see it, you have two choices:

    1. Store the number of tokens in the StringTokenizer before you start to iterate over them:
    Code:
      ...
      int size = stok.countTokens();
      while (stok.hasMoreTokens()) {
        System.out.println(stok.nextToken());
      }
      queue qu = new queue(size);
    2. If you're going to be doing more tokenizing, you can just create a new StringTokenizer object:
    Code:
      ...
      while (stok.hasMoreTokens()) {
        System.out.println(stok.nextToken());
      }
      stok = new StringTokenizer(string);
      queue qu = new queue(stok.countTokens());
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center