如何避免在解析一行CSV中的空位置时触发ArrayIndexOutOfBoundsException? [英] How to avoid triggering an ArrayIndexOutOfBoundsException while parsing empty positions in a line of CSV?

查看:494
本文介绍了如何避免在解析一行CSV中的空位置时触发ArrayIndexOutOfBoundsException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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格式的数据格式为:

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

但是,当我遇到一行数据时, java.lang.ArrayIndexOutOfBoundsException:7

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

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

这是否意味着任何时候我甚至尝试访问 values [7] 的值,将抛出异常?

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?

推荐答案

首先, String.split()不是一个伟大的CSV解析器:它不知道报价,并会搞砸,只要你的一个引用值包含一个逗号。

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.

话虽如此,默认情况下 String.split() 不包含空的尾部元素。您可以使用双参数变量

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

  • 使用正值可以得到最大分割数(意思是除此之外的所有数据都将被分配)单个值,即使它包含逗号)。

  • 0 (如果使用单参数值,则为默认值)数组将根据需要增大,但是空的尾随值将被排除在数组之外(正如你碰到的那样)。

    • -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).
    • 这篇关于如何避免在解析一行CSV中的空位置时触发ArrayIndexOutOfBoundsException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆