【发布时间】:2015-01-01 21:36:09
【问题描述】:
我正在尝试从逗号分隔的 CVS 文件中获取某些值。到目前为止,这是我的代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
class A {
public static void main(String args[]){
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("sample.csv"));
double start = 20659200000.000000;
DecimalFormat df = new DecimalFormat("#.######");
df.format(start);
int i = 0;
while ((sCurrentLine = br.readLine()) != null) {
String[] tmp = sCurrentLine.split(",");
//System.out.println(tmp[0]);
BigDecimal bg1, bg2;
bg1 = new BigDecimal(Double.parseDouble(new
BigDecimal(tmp[0]).toPlainString()));
bg2 = new BigDecimal(start);
if(bg1.equals(bg2))
{
//System.out.println("Inside if");
//System.out.println(tmp[0]);
System.out.println(sCurrentLine);
start = start + 0.000100;
df.format(start);
i = i + 1;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
我正在尝试读取这样的示例文件:
20659200000.000000,1.389320
20659200000.000000,1.318758
20659200000.000000,1.361239
20659200000.000000,1.379709
20659200000.000100,1.389320
20659200000.000100,1.357912
20659200000.000100,1.365672
20659200000.000100,1.374912
20659200000.000100,1.318937
20659200000.000100,1.355331
20659200000.000200,1.370660
20659200000.000200,1.365118
20659200000.000200,1.364933
我只想从左列中选择一个值(第一个)。 目前 if 条件只被访问一次。
我更改了 if 条件和 equals 方法,现在它只适用于前三个迭代。更新起始值时出错。
有什么想法吗? 谢谢。
【问题讨论】:
-
请澄清您的问题。究竟是什么不工作?
-
@dudeprgm 更新了 Q。