我正在尝试从逗号分隔的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方法,现在它仅适用于前三个迭代。更新起始值时出错。
有任何想法吗?
谢谢。
最佳答案
这是您在读取循环中应该做的事情:
while ((sCurrentLine = br.readLine()) != null) {
String[] tmp = sCurrentLine.split(",");
// here only use tmp[0] if you just want to pick the first column
// you could parse it to a double and calculate the stuff you want
System.out.println(tmp[0]);
}
关于java - 比较Java中的十进制值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27734910/