Mudit Jha
Mudit Jha

Reputation: 11

how to seperate a csv file using commas if it has null values and we need data corresponding to upper column?

My code is

        BufferedReader fileReader = new BufferedReader(new FileReader(strFileName));

        while ((strLine = fileReader.readLine()) != null) {
            // [2011.06.28] based on OAP's mail, now ignore "quotations"
            strLine = strLine.replace("\"", "");
            int j = 0;
            String componentID = null;
            String analyteResult = null;
            lineNumber++;
            //break comma separated line using ","
            st = new StringTokenizer(strLine, ",");
            while (st.hasMoreTokens()) {                   
                //st.nextToken();
                String value=st.nextToken();
                if(tokenNumber==1)
                {
                    if(lineNumber==1){
                        batchNo=value;
                    }
                    else if(lineNumber==2){
                        instrumentNo=value;
                    }
                }
                else if(lineNumber==4)
                {
                    if(value.contains("_")){
                        String temp[] = value.split("_");
                        analyteCode = temp[0].trim();
                        a[j] = tokenNumber;
                        j++;
                    }
                }
                else if(lineNumber>4)
                {
                    if(tokenNumber==0){
                        componentID=value;
                    }

                }
                tokenNumber++;
                //System.out.println("Line Number : "+lineNumber+" Token Number: "+tokenNumber+"Value: "+st.nextToken());
            }
            //reset token number
            tokenNumber = 0;

        }

But I need analyte result corresponding to previous columns analyte codes.... as it could be null also....so its vanishing all commas and not parsing the result corresponding to previous column analyte code.

Upvotes: 1

Views: 1195

Answers (1)

Santosh
Santosh

Reputation: 17893

Mudit, I am not sure if you have any compulsion of not using any open sources libs, so I strongly recommend the following widely used open source CSV parsers. It will save you a lot of effort in terms of coding and performance. Pls check them out for your specific need.

  1. Apache Commons CSV parser.
  2. OpenCSV parser

Upvotes: 3

Related Questions