HI,
I am trying to read a pipe (|) delimited file using the StringTokenizer. My problem is that when there is nothing between the pipes the StringTokenizer skips over the field (I would like to return nothing instead of skipping over to the next field.
eg if I have the folloing pipe delimited record
A|B|C
I would expect to get the result
String a = 'A'
String b ='B'
String c = 'C'
and for the following I would expect to see
A||C
String a = 'A'
String b = ''
String c = 'C'
But I am returning:
String a = 'A'
String b = 'C'
String c = ''
please see below for the code any help will be greatly appreciated
Thanks
CODE
public class RestrictedList {
public RestrictedList (SQLConn Uconn,String Y, String M, String D){
String address = "c:\\tmp\\restforb." + Y + M + D;
String thisLine;
String query = null;
String column1Result;
String column2Result;
String countryId;
query = "truncate table restricted_list";
SQLQuery restListquery = new SQLQuery(Uconn, query);
try {
FileReader file = new FileReader(address);
BufferedReader buff = new BufferedReader(file);
String keyField = null;
String symbol = "";
String timestamp = "";
String dateAdded = "";
String timeAdded = "";
String addedBy = "";
String reason = "";
String canBuy = "";
String canSell = "";
String mustBuy = "";
String mustSell = "";
String mustClear = "";
String comment = "";
String longCanBuy = "";
String longCanSell = "";
String longMustSell = "";
String shortCanBuy = "";
String shortCanSell = "";
String ShortMustBuy = "";
boolean eof = false;
if (address.equals("c:\\tmp\\restforb." + Y + M + D)) {
while ((thisLine = buff.readLine()) != null && !eof) {
StringTokenizer st = new StringTokenizer(thisLine, "|"); //think the problem is here but do not know what the syntax is to stop the loop skipping over the empty fields
while (st.hasMoreElements()) {
for (int i = 0; i < 18; i++) {
String field = st.nextToken();
switch (i) {
case 0:
// ric
keyField = field;
symbol = keyField;
break;
case 1:
// timestamp
new String(field);
timestamp = field;
break;
case 2:
// date added
new String(field);
dateAdded = field;
break;
case 3:
// Time added
new String(field);
timeAdded = field;
break;
case 4:
// added by (WQ or MLP)
new String(field);
addedBy = field;
break;
case 5:
// reason
new String(field);
reason = field;
break;
case 6:
// can buy
new String(field);
canBuy = field;
break;
case 7:
// can sell
new String(field);
canSell = field;
break;
case 8:
// must buy
new String(field);
mustBuy = field;
break;
case 9:
// must sell
new String(field);
mustSell = field;
break;
case 10:
// must buy to clear (obsolete)
new String(field);
mustClear = field;
break;
case 11:
// must sell to clear (obsolete)
new String(field);
comment = field;
break;
case 12:
//must clear by date
new String(field);
longCanBuy = field;
break;
case 13:
// comment
new String(field);
longCanSell = field;
break;
case 14:
// long can buy
new String(field);
longMustSell = field;
break;
case 15:
// long can sell
new String(field);
shortCanBuy = field;
break;
case 16:
// long must sell
new String(field);
shortCanSell = field;
break;
case 17:
// short can buy
new String(field);
ShortMustBuy = field;
break;
default:
System.out
.println(new String(field)
+ "Too many data timeseries data types \t");
}
}
query = "INSERT INTO restricted_list & #40;symbol,time_stamp,date,time_added,added_by,reason,can_buy,can_sell,must_buy,
must_sell,must_clear,comment,long_can_buy,long_can_sell,long_must_sell,short_can
_buy,short_can_sell,short_must_sell)" +
" VALUES ('" + symbol +"','"+timestamp +"','"+dateAdded +"',"+timeAdded +",'"+addedBy +"','"+reason +"',"+canBuy +","+canSell +","+mustBuy +","+mustSell +","+mustClear +",'"+comment +"',"+longCanBuy +","+longCanSell +","+longMustSell +","+shortCanBuy +","+shortCanSell +","+ShortMustBuy + ")";
Uconn.execute(query);
}
}
} else {
System.out
.println("File address "
+ address
+ " not correct, "
+ "please make sure instrument file path is present and correct");
}
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
}
}