0

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.

user340
  • 375
  • 12
  • 28

5 Answers5

1

Here is what you should do in your read loop:

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]);
}
Paul
  • 66
  • 3
1

You can't compare two doubles in this way. There is a loss of precision when converting to a double that means that two values that appear to be equal might end up being different, and two values that appear different might end up being the same.

What you should do is to store the value you want as a String, and then compare the String value you're reading in with the one you've stored, or else use the BigDecimal.equals() method to test for equality between two BigDecimal instances.

Comparing two Strings will work only if you know they're stored in exactly the same format, of course (same number of decimal places and so on). Comparing two BigDecimal instances will work provided that you have values that can be represented exactly as a BigDecimal, and since you're reading your values from a text file, that really ought to be the case!

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
1

Actually here the if condition will be accessed once, cause in the condition you wrote tmp[i]. After the first iteration the i changes to 1. Then at next iteration tmp[i] value changes to 1.318758, which is the second column value of second row.

So the condition does not satisfied and breaks.

What you can do is put the int i = 0; after the while condition.

Anirban Nag 'tintinmj'
  • 5,572
  • 6
  • 39
  • 59
0

You should use modulus with your increment inside the if statement.

However, why are you not accessing the first column directly using

So...

` while ((sCurrentLine = br.readLine()) != null) {

      String[] tmp = sCurrentLine.split(",");
      if(tmp.length > 0){
             System.out.println("The first column is: " + tmp[0]);
      }
}`
mattsap
  • 3,790
  • 1
  • 15
  • 36
0

Better go this way:

        String start = "20659200000.000000";
        while ((sCurrentLine = br.readLine()) != null) {
            String[] tmp = sCurrentLine.split(",");
            if(tmp[0].equals(start)) {
                System.out.println("Inside if");
            }
        }
Divers
  • 9,531
  • 7
  • 45
  • 88