Java CSV File Parse parseCSVIntegers(String csv)

Here you can find the source of parseCSVIntegers(String csv)

Description

Parses a CSV string into a list of integers

License

Open Source License

Parameter

Parameter Description
csv the string

Exception

Parameter Description
NumberFormatException if one of the numbers is invalid

Return

the list of integers

Declaration

public static List<Integer> parseCSVIntegers(String csv) 

Method Source Code

//package com.java2s;
/**//from  w w  w  . j a  va 2s .co m
 * Copyright 2011 Rowan Seymour
 * 
 * This file is part of Kumva.
 *
 * Kumva is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Kumva is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Kumva. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Parses a CSV string into a list of integers
     * @param csv the string
     * @return the list of integers
     * @throws NumberFormatException if one of the numbers is invalid
     */
    public static List<Integer> parseCSVIntegers(String csv) {
        String[] vals = csv.split(",");
        List<Integer> ints = new ArrayList<Integer>();
        for (String val : vals) {
            String v = val.trim();
            if (v.length() > 0) {
                Integer n = Integer.parseInt(v);
                if (n != null)
                    ints.add(n);
            }
        }
        return ints;
    }
}

Related

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