0

I have a CSV file which I am parsing using OpenCSV. The CSV file looks like the following:

delta,theta,signal_1,signal_2,signal_3,signal_4,signal_5,signal_6,value_1,value_2,death 71239,56366,8654,21170,16053,27229,5835,8834,3.511244,3.511244,0 234145,160964,16164,20320,29410,17088,9041,8885,5.473104,4.165198,0 76576,23670,11216,22239,8831,15797,13216,7872,2.680331,2.380573,0 44504,39475,12008,18694,14639,25165,11716,24879,2.696564,2.685185,0 275603,31948,10360,7867,15249,16791,8526,4918,2.095088,2.490662,0 80893,16847,7368,19869,5425,11039,6720,5553,3.105438,2.632364,0 70494,323693,61661,3887,17667,35640,16069,8686,18.3219,9.953226,1 808498,57853,13771,15957,13831,20629,15981,11177,4.18285,10.971,1 900628,263343,38948,48837,11122,12378,29276,25673,23.67767,15.39414,1 306369,11836,12448,16708,14225,38644,56110,24842,0.8320562,9.564192,1 2337196,354879,28332,94271,31804,40325,27890,35445,11.15831,11.88935,1 129047,45750,2640,13725,6823,17161,8363,8970,6.705262,6.231878,1 275603,31948,10360,7867,15249,16791,8526,4918,2.095088,2.490662,2 80893,16847,7368,19869,5425,11039,6720,5553,3.105438,2.632364,2 80494,312693,61261,3887,17667,35640,16069,8686,18.3219,9.953226,2 990628,123343,12948,48837,11122,12378,29276,25673,23.67767,15.39414,2 886369,56836,12348,16708,14225,38644,56110,24842,0.8320562,9.564192,2 4437196,234879,34532,94271,31804,40325,27890,35445,11.15831,11.88935,2

Here, the last column is death value and that keep increasing after certain rows (which is totally arbitrary). I would like to find in which row the first death occurred and take the previous 3 rows and write it into a separate csv file. So here, the first death occurs at

70494,323693,61661,3887,17667,35640,16069,8686,18.3219,9.953226,1

So I will consider the preceding three rows:

44504,39475,12008,18694,14639,25165,11716,24879,2.696564,2.685185,0 275603,31948,10360,7867,15249,16791,8526,4918,2.095088,2.490662,0 80893,16847,7368,19869,5425,11039,6720,5553,3.105438,2.632364,0

and write them in to a separate CSV File (suppose this is newCSV.csv). And will do the same with death = 2 and death = 3 and keep appending them in to that newCSV.csv file. I can use the OpenCSV to read them line by line but not getting any idea how to detect the rows based on the death value. Any help would be appreciated.

0

1 Answer 1

1

Here, is your complete solution,

Your Main Logic,

FileInputStream fin = new FileInputStream("C/inputFile.csv");
        FileOutputStream fout = new FileOutputStream("C/newCSV.csv");

        BufferedReader br = new BufferedReader(new InputStreamReader(fin));

        MyObject[] last3MyObject = new MyObject[3];
        int index = 0;
        String str = br.readLine();//read Filed Header Value...
        String fieldHeader = str;
        fout.write(fieldHeader.getBytes());//write header into output file...

        str = br.readLine();

        int currentDeathValue = 0;
        int previousDeathValue = 0;

        while(str != null){

            MyObject myObject = new MyObject();

            String [] tkn = str.split(",");

            myObject.setDelta(Long.parseLong(tkn[0]));
            myObject.setTheta(Long.parseLong(tkn[1]));
            myObject.setSignal_1(Long.parseLong(tkn[2]));
            myObject.setSignal_2(Long.parseLong(tkn[3]));
            myObject.setSignal_3(Long.parseLong(tkn[4]));
            myObject.setSignal_4(Long.parseLong(tkn[5]));
            myObject.setSignal_5(Long.parseLong(tkn[6]));
            myObject.setSignal_6(Long.parseLong(tkn[7]));
            myObject.setValue_1(Double.parseDouble(tkn[8]));
            myObject.setValue_2(Double.parseDouble(tkn[9]));

            currentDeathValue = Integer.parseInt(tkn[10]);

            if(currentDeathValue != previousDeathValue){
                for(MyObject mylast3Object  : last3MyObject){

                    fout.write(mylast3Object.toString().getBytes());
                    fout.write("\n".getBytes());
                }
            }

            previousDeathValue = currentDeathValue;

            myObject.setDeath(currentDeathValue);

            if(index == 3){
                index = 0;
            }

            last3MyObject[index++] = myObject; 

            str = br.readLine();

Make one Bean Class like,

class MyObject{

    private long delta;
    private long theta;
    private long signal_1;
    private long signal_2;
    private long signal_3;
    private long signal_4;
    private long signal_5;
    private long signal_6;
    private double value_1;
    private double value_2;
    private int death;
    public long getDelta() {
        return delta;
    }
    public void setDelta(long delta) {
        this.delta = delta;
    }
    public long getTheta() {
        return theta;
    }
    public void setTheta(long theta) {
        this.theta = theta;
    }
    public long getSignal_1() {
        return signal_1;
    }
    public void setSignal_1(long signal_1) {
        this.signal_1 = signal_1;
    }
    public long getSignal_2() {
        return signal_2;
    }
    public void setSignal_2(long signal_2) {
        this.signal_2 = signal_2;
    }
    public long getSignal_3() {
        return signal_3;
    }
    public void setSignal_3(long signal_3) {
        this.signal_3 = signal_3;
    }
    public long getSignal_4() {
        return signal_4;
    }
    public void setSignal_4(long signal_4) {
        this.signal_4 = signal_4;
    }
    public long getSignal_5() {
        return signal_5;
    }
    public void setSignal_5(long signal_5) {
        this.signal_5 = signal_5;
    }
    public long getSignal_6() {
        return signal_6;
    }
    public void setSignal_6(long signal_6) {
        this.signal_6 = signal_6;
    }
    public double getValue_1() {
        return value_1;
    }
    public void setValue_1(double value_1) {
        this.value_1 = value_1;
    }
    public double getValue_2() {
        return value_2;
    }
    public void setValue_2(double value_2) {
        this.value_2 = value_2;
    }
    public int getDeath() {
        return death;
    }
    public void setDeath(int death) {
        this.death = death;
    }

    @Override
    public String toString() {
        return this.delta+","+this.theta+","+this.signal_1+","+this.signal_2+","+this.signal_3+","+this.signal_4+","+this.signal_5+","+this.signal_6+","+this.value_1+","+this.value_2+","+this.death;
    }

}

EDITED....

while(str != null){
            //System.out.println("IN");
            MyObject myObject = new MyObject();

            String [] tkn = str.split(",");

            myObject.setDelta(Long.parseLong(tkn[0]));
            myObject.setTheta(Long.parseLong(tkn[1]));
            myObject.setSignal_1(Long.parseLong(tkn[2]));
            myObject.setSignal_2(Long.parseLong(tkn[3]));
            myObject.setSignal_3(Long.parseLong(tkn[4]));
            myObject.setSignal_4(Long.parseLong(tkn[5]));
            myObject.setSignal_5(Long.parseLong(tkn[6]));
            myObject.setSignal_6(Long.parseLong(tkn[7]));
            myObject.setValue_1(Double.parseDouble(tkn[8]));
            myObject.setValue_2(Double.parseDouble(tkn[9]));
            currentDeathValue = Integer.parseInt(tkn[10]);
            myObject.setDeath(currentDeathValue);

            if(flag && (previousDeathValue == currentDeathValue)){
                if(index < 3){
                    last3MyObject[index++] = myObject;
                }else{
                    flag = false;
                    for(MyObject mylast3Object  : last3MyObject){
                        System.out.println(mylast3Object);
                        fout.write(mylast3Object.toString().getBytes());
                        fout.write("\n".getBytes());
                    }

                }
            }

            if(currentDeathValue != previousDeathValue){
                flag = true;
                index = 0;
            }

            previousDeathValue = currentDeathValue;

            str = br.readLine();
        }
5
  • That worked like a charm. Thanks a lot. But only for the first death occurrence I am getting 4 rows instead of 5. Still trying to figure out why ! Is that because of the header in my CSV file?
    – SaberTooth
    Nov 27, 2015 at 9:20
  • Sorry that was easy to solve. Just added a newline after the header !
    – SaberTooth
    Nov 27, 2015 at 9:43
  • Any idea on how can I do this if I consider for extracting next 3 rows. Like when I found one death occurrence then I will consider the next 3 rows after the death row. So for my csv file I have to write these 3 rows for the first death occurrence: 808498,57853,13771,15957,13831,20629,15981,11177,4.18285,10.971,1 900628,263343,38948,48837,11122,12378,29276,25673,23.67767,15.39414,1 306369,11836,12448,16708,14225,38644,56110,24842,0.8320562,9.564192,1 Any help @vishal gajera ?
    – SaberTooth
    Nov 28, 2015 at 12:51
  • @Rasam , sure we can do that one too. see my EDITED section. just need to change while loop into my previous one answer. Nov 28, 2015 at 20:09
  • Thanks a lot for your time and effort. I already did that part in a slightly different way though
    – SaberTooth
    Nov 30, 2015 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.