读入时跳过空行
【问题】
I have an arraylist of records that is from an input csv file. Some records have full columns but some have null. For example:
abc, 1441652452, 8.64015, 52.75034
abc, 1442279677,,
abc, 1442280570, 10.44255,148.78166
anyways, there could be more than one line of in complete records consecutively. if you look at the records, at position [2] and [3] lat and longs are being stored accordingly. what i want to do is to calculate the distance between (in the case of my example above) the third record and the fist record. (I already have the formula to calculate the distance between latlngs. Currently, this is what my code consists of:
for (int i = 0; i < oneUserRecord.size(); i++) {
String currentRecord = oneUserRecord.get(i);
String[] current = currentRecord.split(",");
double currentLat = Double.parseDouble(current[3]);
double currentLng = Double.parseDouble(current[4]);
}
What I initially planned to do was to retrieve the next record, and extract the next record's lat and long, and then find out the distance between the next and the current records' lat and longs. However, because of the incomplete records I thus won't be able to extract positions [3] and [4] simply because they don't exist in those strings. So, my question simply put is, how do I check whether positions [3] and [4] of the next record is empty, if it is empty, how do i assign the next record to be the following row of record to have a value for lat and long?
I had a logic, (continuing from the code above),
String nextRecord = "";
if ((oneUserRecord.get(i++).split(",")[3]).isEmpty()
|| (oneUserRecord.get(i++).split(",")[4]).isEmpty()) {
nextRecord = oneUserRecord.get(i++);
}
else {
nextRecord = oneUserRecord.get(i + 1);
}
String[] next = nextRecord.split(",");
double nextLat = Double.parseDouble(next[3]);
double nextLng = Double.parseDouble(next[4]);
but it didn't work, still got an out of bounds error. Is the a way I can improve on this, or another method for me to get the result I want?
【回答】
可以先把有空值的行过滤掉,然后就不用考虑相邻行比较的事了。可用SPL做这个事并嵌入到Java中做进一步处理:
ResultSet rs = st.executeQuery("=file("d:\\source.csv").import@c().select(#3==null || #4==null)"
不同的是,SPL返回的结果是JDBC标准的ResultSet对象,JAVA代码也可直接遍历。