Java CSV File Parse parseCSVLine(String CSVLine, char delimChar, char quotChar)

Here you can find the source of parseCSVLine(String CSVLine, char delimChar, char quotChar)

Description

parse CSV Line

License

Apache License

Declaration

public static ArrayList<String> parseCSVLine(String CSVLine,
            char delimChar, char quotChar) 

Method Source Code

//package com.java2s;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * Copyright 2013 Joseph Yuan/*from  www  . ja v  a2s.  c  o  m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

import java.util.ArrayList;

public class Main {
    public static ArrayList<String> parseCSVLine(String CSVLine,
            char delimChar, char quotChar) {
        char itr;
        boolean inQuotedValue = false;
        String buffer = "";
        ArrayList<String> parsedLine = new ArrayList<String>();
        for (int i = 0; i < CSVLine.length(); i++) {
            itr = CSVLine.charAt(i);
            if (itr == delimChar) {
                if (!inQuotedValue) {
                    parsedLine.add(buffer);
                    buffer = "";
                }
            } else if (itr == quotChar) {
                inQuotedValue = !inQuotedValue;
            } else {
                buffer += itr;
            }
        }
        if (buffer.length() > 0) {
            parsedLine.add(buffer);
        }
        String entry;
        for (int i = 0; i < parsedLine.size(); i++) {
            entry = parsedLine.get(i);
            entry.trim();
            if (entry.startsWith("(") && entry.endsWith(")")
                    && isNumeric(entry.substring(1, entry.length() - 1))) {
                parsedLine.set(i,
                        "-" + entry.substring(1, entry.length() - 1));
            }
        }
        return parsedLine;
    }

    public static boolean isNumeric(String str) {
        String s = str;
        if (s.startsWith("-")) {
            s = s.substring(1);
        }
        char c;
        int i, L = s.length(), sinceLastComma = 0;
        boolean decimalHit = false, commaHit = false;
        for (i = 0; i < L; i++) {
            c = s.charAt(i);
            if (c < '0' || c > '9') {
                if (c == '.' && !decimalHit) {
                    decimalHit = true;
                    if (commaHit && sinceLastComma != 3) {
                        return false;
                    }
                    continue;
                } else if (c == ',' && !decimalHit) {
                    if (commaHit) {
                        if (sinceLastComma != 3) {
                            return false;
                        }
                        sinceLastComma = 0;
                    }
                    commaHit = true;
                    continue;
                }
                return false;
            } else {
                if (commaHit)
                    sinceLastComma++;
            }
        }
        return true;
    }

    public static boolean isNumeric(char c) {
        return '0' <= c && '9' >= c;
    }
}

Related

  1. parseCSV(String csvString)
  2. parseCSV(String data, char... ch)
  3. parseCsvFile(String filename, String csvSplitBy, boolean skipHeader)
  4. parseCSVIntegers(String csv)
  5. parseCsvLine(final String line)
  6. parseCsvRecord(String record, char csvSeparator)
  7. parseCsvString(String toParse)
  8. parseExcelCSVLine(String line)
  9. parseLine(String csvLine)