简体   繁体   中英

How can I get a correct string split by coma on Java?

I have the next csv file:

hotel,is_canceled,lead_time, ..
Resort Hotel,0,300, ..
Resort Hotel,0,

And the problem comes when, for example, i want an ArrayList with the first column:

[Resort Hotel, Resort Hotel, .. ]

To do that i read the file row by row, on each row (it's an entire string row) i use row.split(","). But the result i get from doing that is like this: [Resort, Hotel, Resort, Hotel, ...] when i want [Resort Hotel, Resort Hotel, ...] I've tried using split("\,") and split(Pattern.quote(",")) but none of those work. Dont know why it splits the data also when there's a space, not only with coma.

Any idea? I'll put here the code in case it's an error on my code. What i do first is use a Scanner to read the file, then (on the first while) i search for a concrete attribute (in this case hotel) and on the second while i get row by row splitting the row in comas and grabbing the one attribute i want, adding it to the result Array.

public String[] read_column_scan(String file, String atribut) {
    File _file = new File(file);
    try {
        Scanner inputStream = new Scanner(_file);
        String data;
        data = inputStream.next();
        String[] values = data.split(",");
        boolean found = false;
        int i = -1;
        while(!found && (i < values.length)) {
            ++i;
            if(values[i].equals(atribut)) found = true;
        }
        ArrayList<String> result_aux = new ArrayList<String>();
        String[] values2;
        while(inputStream.hasNext()) {
            data = inputStream.next();
            values2 = data.split(",");
            String aux = values2[i];
            result_aux.add(aux);
        }
        String[] result = new String[result_aux.size()];
        result_aux.toArray(result);
        return result;
    } catch(Exception e) {
        e.printStackTrace();
    }
    return null;
}

Method next() in class Scanner reads the next token and the default delimiter for a token is whitespace. You want the Scanner to read an entire line of the CSV file. Hence you require method hasNextLIne() to see if there is another line in the file, and method nextLine() to read an entire line. Then your split(",") will work because your string is an entire line and not just a word.

public String[] read_column_scan(String file, String atribut) {
    File _file = new File(file);
    try {
        Scanner inputStream = new Scanner(_file);
        String data;
        data = inputStream.nextLine(); // change here
        String[] values = data.split(",");
        boolean found = false;
        int i = -1;
        while(!found && (i < values.length)) {
            ++i;
            if(values[i].equals(atribut)) found = true;
        }
        ArrayList<String> result_aux = new ArrayList<String>();
        String[] values2;
        while(inputStream.hasNextLine()) { // change here
            data = inputStream.nextLine(); // change here
            values2 = data.split(",");
            String aux = values2[i];
            result_aux.add(aux);
        }
        String[] result = new String[result_aux.size()];
        result_aux.toArray(result);
        return result;
    } catch(Exception e) {
        e.printStackTrace();
    }
    return null;
}

You will save yourself alot of pain if you use a CSV parser library instead of trying to parse it yourself. You are already missing the proper handling of quoted values.

I recommend using OpenCSV http://opencsv.sourceforge.net/#even_quicker_start

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