Click to See Complete Forum and Search --> : how do u do a count
P Lee
Oct 21st, 2002, 09:42 AM
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.
CornedBee
Oct 22nd, 2002, 06:52 AM
StringTokenizer comes to mind.
Dillinger4
Oct 22nd, 2002, 02:05 PM
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.
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);}
}
}
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.