Results 1 to 6 of 6

Thread: Problem in StringTokenizer

  1. #1
    Join Date
    Dec 2009
    Posts
    292

    Problem in StringTokenizer

    Hello,
    I read an entire text file at once. I put in a table all the words of the text.
    So I'm taking a tokenizer as a separate space. Unfortunately, when there is a newline, it works more. For it is not a space and yet it should be treated as such. Here is my code:
    Code:
     StringTokenizer stok;
         stok = new StringTokenizer(chain); / / Apply the StringTokenizer chain
         while(stok.hasMoreTokens()){        / / Returns false when reading the lastok word
             nMots = stok.countTokens();     / / Indicates the number of words remaining in stok
    
             s = stok.nextToken();           / / Read a stokring in the next word
         }
    I think I am facing a problem in StringTokenizer. Any help is appreciated. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    Re: Problem in StringTokenizer

    Hello,
    You can do something like:
    Code:
    String[] t = myString.split("\\s + ");
    Provided utilliser java 1.4 or later version of java. The regular expression will probably improve but I think it may be the case. If you need any help regarding this you can post here and we will try to solve your problem. And also if you have an error then post the error so that we can help you by errors.

  3. #3
    Join Date
    Nov 2009
    Posts
    335

    Re: Problem in StringTokenizer

    Hello,
    I work already vec StringTokenizer. Here is the solution and I'm sure it just:
    Code:
     StringTokenizer stok;
         stok = new StringTokenizer(chain " \ b\ r\ n"); / / Apply the StringTokenizer chain
         while(stok.hasMoreTokens()){        / / Returns false when reading the lastok word
             nMots = stok.countTokens();     / / Indicates the number of words remaining in stok
    
             s = stok.nextToken();           / / Read a stokring in the next word
         }

  4. #4
    Join Date
    Nov 2009
    Posts
    446

    Re: Problem in StringTokenizer

    Hello,
    The example is correct and I would like to add something to it. In the 2nd parameter c the separator, the first 2 are white spaces and 2 c the last line breaks. But as the separator that are not entangled in the table to take them we must add a third parameter in the constructor:
    Code:
     StringTokenizer stok;
         stok = new StringTokenizer(chain " \ b\ r\ n", true); / / Apply the StringTokenizer chain
    the rest is the same. I hope you are getting the point what I am try to explain to you.
    Last edited by cyber-noob; 18-02-2010 at 04:38 AM.

  5. #5
    Join Date
    Nov 2009
    Posts
    335

    Re: Problem in StringTokenizer

    Hello,
    I would add the TAB character as a separator. I think you should use this in your code.
    Code:
    st = new StringTokenizer(chain " \ b\ t\ r\ n"); / / Apply the StringTokenizer chain
    If you need more information on the StringRokenizer then you can visit the sun's official site and there you can find the detailed information on this.

  6. #6
    Join Date
    Dec 2009
    Posts
    296

    Re: Problem in StringTokenizer

    Hello,
    I am new to this concept and I have tried some code on this
    Code:
    StringTokenizer strok=new StringTokenize(some,"agent>");
    
    while(strok.hasMoreTokens())
    {
        String token = strok.nextToken();
        System.out.println("output ="+token);
    }
    See, if the code is useful to you and if not then you can let it go.

Similar Threads

  1. StringTokenizer problem in java
    By Jensen Ackles in forum Software Development
    Replies: 5
    Last Post: 06-04-2010, 03:16 PM
  2. Retrieve an array using StringTokenizer
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 12:57 AM
  3. StringTokenizer class of java
    By Mithun Seth in forum Software Development
    Replies: 5
    Last Post: 10-02-2010, 01:12 PM
  4. Problem with StringTokenizer
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 07-02-2010, 01:47 AM
  5. FileInputStream and StringTokenizer
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 28-01-2010, 12:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,940,393.47433 seconds with 16 queries