解析 CSV 行中的空位置时如何避免触发 ArrayIndexOutOfBoundsException?

发布于 2024-11-18 15:44:20 字数 958 浏览 7 评论 0原文

String[] values = line.split(",");

Long locId = Long.parseLong(replaceQuotes(values[0]));
String country = replaceQuotes(values[1]);
String region = replaceQuotes(values[2]);
String city = replaceQuotes(values[3]);
String postalCode = replaceQuotes(values[4]);
String latitude = replaceQuotes(values[5]);
String longitude = replaceQuotes(values[6]);
String metroCode = replaceQuotes(values[7]);
String areaCode = replaceQuotes(values[8]);

//...

public String replaceQuotes(String txt){
    txt = txt.replaceAll("\"", "");
    return txt;
}

我使用上面的代码来解析包含以下格式数据的 CSV:

828,"US","IL","Melrose Park","60160",41.9050,-87.8641,602,708

但是,当我遇到如下所示的一行数据时,我得到 java.lang.ArrayIndexOutOfBoundsException: 7< /strong>

1,"O1","","","",0.0000,0.0000,,

这是否意味着每当我尝试访问 values[7] 处的值时,都会抛出异常?

如果是这样,如何解析文本行该位置不包含数据的行?

String[] values = line.split(",");

Long locId = Long.parseLong(replaceQuotes(values[0]));
String country = replaceQuotes(values[1]);
String region = replaceQuotes(values[2]);
String city = replaceQuotes(values[3]);
String postalCode = replaceQuotes(values[4]);
String latitude = replaceQuotes(values[5]);
String longitude = replaceQuotes(values[6]);
String metroCode = replaceQuotes(values[7]);
String areaCode = replaceQuotes(values[8]);

//...

public String replaceQuotes(String txt){
    txt = txt.replaceAll("\"", "");
    return txt;
}

I'm using the code above to parse a CSV with data in this format:

828,"US","IL","Melrose Park","60160",41.9050,-87.8641,602,708

However, when I encounter a line of data such as the following I get java.lang.ArrayIndexOutOfBoundsException: 7

1,"O1","","","",0.0000,0.0000,,

Does this mean that any time I even try to access the value at values[7], an Exception will be thrown?

If so, how do I parse lines that don't contain data in that position of the text line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追我者格杀勿论 2024-11-25 15:44:20

首先,String.split() 不是一个出色的 CSV 解析器:它不知道引号,并且一旦引用值之一包含逗号,它就会陷入混乱。

话虽这么说,默认情况下 String.split() 省略空尾随元素。您可以通过使用 双参数变体

String[] values = line.split(",", -1);
  • -1(或任何负值)意味着数组将根据需要尽可能大。
  • 使用正值给出了要完成的最大分割量(这意味着超出该值的所有内容都将是单个值,即使它包含逗号)。
  • 0(如果使用单参数值,则为默认值)意味着数组将根据需要尽可能大,但是空尾随值将被排除在数组之外(正如发生在您身上的情况一样)。

First of all, String.split() is not a great CSV parser: it doesn't know about quotes and will mess up as soon as one of your quoted values contains a comma.

That being said, by default String.split() leaves out empty trailing elements. You can influence that by using the two-argument variant:

String[] values = line.split(",", -1);
  • -1 (or any negative value) means that the array will be as large as necessary.
  • Using a positive value gives a maximum amount of splits to be done (meaning that everything beyond that will be a single value, even if it contains a comma).
  • 0 (the default if you use the one-argument value) means that the array will be as large as necessary, but empty trailing values will be left out of the array (exactly as it happens to you).
停滞 2024-11-25 15:44:20

作为一般规则,如果已经存在可用的解析器,那么您永远不应该破解自己的(有故障的)解析器。 CSV 不容易正确解析,并且 String.split 无法完成这项工作,因为 CSV 允许在 " 之间使用 , 而无需充当分隔符。

请考虑使用 < a href="http://opencsv.sourceforge.net/" rel="nofollow">OpenCSV 这将解决您现在遇到的问题以及用户使用 时将面临的问题。 , 作为数据的一部分。

As a general rule you should never, ever hack up your own (faulty) parser if a working one already exists. CSV is not easy to parse correctly, and String.split will not do the job since CSV allows , to be used between "'s without working as separaters.

Consider using OpenCSV. This will solve both the problem you have now and the problem you will face when a user uses a , as part of the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文