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.
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
Bookmarks