I am trying to get certain values from a comma separated CVS file. So far this is my code:
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();
}
}
}
}
I am trying to read a sample file like this:
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
I want to pick only one value (the first) from the left column. Currently the if condition is accessed only once.
I changed the if condition and the equals method, now it only works for the first three iterations. There is an error in updating the start value.
Any ideas? Thanks.