简体   繁体   English

如何在 Java 上获得正确的逗号分割字符串?

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

I have the next csv file:我有下一个 csv 文件:

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:例如,当我想要一个带有第一列的 ArrayList 时,问题就来了:

[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(",").为此,我逐行读取文件,在每一行(它是整个字符串行)上,我使用 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.但是我这样做的结果是这样的: [Resort, Hotel, Resort, Hotel, ...]当我想要[Resort Hotel, Resort Hotel, ...]我尝试使用 split("\," ) 和 split(Pattern.quote(",")) 但这些都不起作用。 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. class Scanner中的方法next()读取下一个标记,标记的默认分隔符是空格。 You want the Scanner to read an entire line of the CSV file.您希望Scanner读取 CSV 文件的整行。 Hence you require method hasNextLIne() to see if there is another line in the file, and method nextLine() to read an entire line.因此,您需要方法hasNextLIne()来查看文件中是否还有另一行,并需要方法nextLine()来读取整行。 Then your split(",") will work because your string is an entire line and not just a word.然后您的split(",")将起作用,因为您的字符串是一整行而不仅仅是一个单词。

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.如果您使用 CSV 解析器库而不是尝试自己解析它,您将为自己省去很多痛苦。 You are already missing the proper handling of quoted values.您已经错过了对引用值的正确处理。

I recommend using OpenCSV http://opencsv.sourceforge.net/#even_quicker_start我推荐使用 OpenCSV http://opencsv.sourceforge.net/#even_quicker_start

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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