简体   繁体   中英

How to handle commas in the data of a csv in Java

I'm using scanner.delimiter to split my csv, with the delimiter being ",". However I have some data that includes commas in the data like "Monsters, Inc."

However if I set the delimiter to "\\",\\"", then it crashes on everything else.

Ideas that don't require me to write my own scanner.delimiter method?

I don't think scanner.delimiter will work for this kind of problem. If you have quotes in the data where the extra comas exist, you can either use a regular expression or code to solve this kind of problem using also the String.split as mentioned in similar answers/questions. If you don't have quotations, then there is really nothing you can do.

There have been similar examples on stackoverflow. For example I think this one applies to you:

Splitting a csv file with quotes as text-delimiter using String.split()

Using Split

public static void main(String[] args) {
    String s = "Sachin,,M,\"Maths,Science,English\",Need to improve in these subjects.";
    String[] splitted = s.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
    System.out.println(Arrays.toString(splitted));
}

Using custom code

public static ArrayList<String> customSplitSpecific(String s)
{
    ArrayList<String> words = new ArrayList<String>();
    boolean notInsideComma = true;
    int start =0, end=0;
    for(int i=0; i<s.length()-1; i++)
    {
        if(s.charAt(i)==',' && notInsideComma)
        {
            words.add(s.substring(start,i));
            start = i+1;                
        }   
        else if(s.charAt(i)=='"')
        notInsideComma=!notInsideComma;
    }
    words.add(s.substring(start));
    return words;
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM