Results 1 to 3 of 3

Thread: how do u do a count

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    London, Uk.
    Posts
    72

    Angry how do u do a count

    Hey,

    I have a .txt file which contains general book order info such as:

    customer name: XXX,
    book title(s) ordered: XXX,
    order date: XXX,

    which the contents are read in when java program is run, how do I do a count on for example the number of books ordered?

    Thanks.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    StringTokenizer comes to mind.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Yeah CornedBee is right. You could use a string StringTokenizer.
    The data stored in your text file can be delimited by characters other then spaces if you want by passing in a string to the StringTokenizer as the second arguement. The problem that i found using a StringTokenizer though is that everything is parsed and returned. For instance if your data is set up as such.

    customer name: John
    book title(s) ordered: 125
    order date: 09/11/02

    you will get the following.

    customer name
    John
    book title(s) ordered
    125
    order date
    09/11/02

    The code that produces the previous output is the following.
    Code:
      import java.io.*; 
      import java.util.*;
      
    class Grabber{ 
        public static void main(String[] args){
    
       try{
       File f = new File("C://Java/text.txt");
       BufferedReader buff = new BufferedReader(new FileReader(f));
       
       while(true){
           String x = buff.readLine();
            if(x == null) break; 
              StringTokenizer st = new StringTokenizer(x,":"); 
                while(st.hasMoreTokens()){
                   System.out.println(st.nextToken()); 
          }
        }
       }catch(IOException e){System.err.println(e);}
      }
     }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width