String tokenizer


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: String tokenizer

  1. #1
    Join Date
    Mar 2004
    Posts
    2

    Unhappy String tokenizer

    Hi all,

    I've been having problems with string tokenizers recently. Basically i'm reading in a text file and then trying to extract the pieces of information I need.

    the file that I read in is:
    ---------------------------------------------------------------
    ##
    ## Note: any line starting with two ## symbols is a ~~
    ## comment
    ##
    ## Begin update line includes the ID of the rep which ## generated this file
    ## Begin order line includes order id, shop id and date
    ## All other lines are order detail lines which include
    ## book id, quantity and selling prices
    ##
    BEGIN-UPDATE REP 1
    BEGIN-ORDER 1 SHOP 1 DATE 2004-02-27
    1 10 3.99
    7 5 14.99
    3 20 0.99
    8 10 3.99
    END-ORDER
    BEGIN-ORDER 2 SHOP 3 DATE 2004-03-01
    1 5 3.99
    5 10 10.99
    8 40 2.99
    END-ORDER
    END-UPDATE
    ------------------------------------------------------------

    (i included this file as an attachment aswell)

    I need to ignore anything begining with ## as these are comments and need to store the repID(number after REP), the order number(number after BEGIN-ORDER,etc), the shop number, the date and then each of the sets of three values between the BEGIN-ORDER and END-ORDER delimeters. I then need to send the 3 values and the rep id, order id,shop id and date to another function where I store them in a database(i have no problem with this part).

    I've tryed to do this using a string tokenizer and its been driving me nutz. I dont think its too difficult but I just cant get it to work.

    Can anyone help? any suggestions or ideas how I should go about it would be greatly appreciated.

    Thanks
    Attached Files Attached Files

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    Once you've read a whole line into a String:
    Code:
    if (string.startsWith("##"))
        ignore the line
    That should get you headed in the right direction. It's the easiest way I can think of.

  3. #3
    Join Date
    Feb 2004
    Posts
    808
    to go a bit further:

    Code:
     
      StringTokenizer st;
      String line;
    
      String token1;
      String token2;
    
    
      for(line = inputReader.readLine(); line!=null; line=inputReader.readLine()){
    
      if line.startsWith("##")
        continue; //on to the next loop
      
      st = new StringTokenizer(line, " ");
    
      token1 = st.nextToken();
      token2 = st.nextToken();
    
    
      if(token1.equals("BEGIN-UPDATE") && token2.equals("REP")){
        //do whatever
      }else if(token1.equals("BEGIN-OREDER") && token2.equals("....")
        Order myOrder = new Order(however you make it)
        doOrder(myOrder);
    etc etc.. the doOrdermethod should repeatedly (loop) readline, FROM THE SAME READER (make it class wide) and add items to the order until it happens upon an "END ORDER" command...

    ntoes:
    ive only done 3 token strings.. you may need more e.g. for the line:


    BEGIN-ORDER 1 SHOP 1 DATE 2004-02-27

    has.. er.. 6 tokens... only the first 2 count as far as deciding what IF to do.. but when youre inside the IF that deals with orders, you can nextToken() 4 more times to get the info out that you want. DONT be tempted to do all 6 at first, because not all lines have 6 tokens

    that any help?
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

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