Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:7表示索引正确的数组_Java_Arrays_Csv_Bufferedreader_Indexoutofboundsexception - Fatal编程技术网

“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:7表示索引正确的数组

“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:7表示索引正确的数组,java,arrays,csv,bufferedreader,indexoutofboundsexception,Java,Arrays,Csv,Bufferedreader,Indexoutofboundsexception,我有一个包含8列的CSV文件: 我用下面的代码将每列放入一个数组中 public static void main(String args[]) { List<String> wholefile = new ArrayList<String>(); List<String> id = new ArrayList<String>(); List<String> property_address = new Ar

我有一个包含8列的CSV文件:

我用下面的代码将每列放入一个数组中

public static void main(String args[]) {

    List<String> wholefile = new ArrayList<String>();
    List<String> id = new ArrayList<String>();
    List<String> property_address = new ArrayList<String>();
    List<String> first_name = new ArrayList<String>();
    List<String> last_name = new ArrayList<String>();
    List<String> email = new ArrayList<String>();
    List<String> owner_address = new ArrayList<String>();
    List<String> price = new ArrayList<String>();
    List<String> date_sold = new ArrayList<String>();


    Path filepath = Paths.get("./data.csv");


    try {

      BufferedReader br = new BufferedReader(new FileReader("./data.csv"));
       String line;
       while ((line = br.readLine()) != null) {
         wholefile.add(line);
         String[] cols = line.split(",");
         id.add(cols[0]);
         property_address.add(cols[1]);
         first_name.add(cols[2]);
         last_name.add(cols[3]);
         email.add(cols[4]);
         owner_address.add(cols[5]);
         price.add(cols[6]);
       }
      System.out.println(id);
      System.out.println(property_address);
      System.out.println(first_name);
      System.out.println(last_name);
      System.out.println(email);
      System.out.println(owner_address);
      System.out.println(price);


   } catch (IOException e) {
       e.printStackTrace();
   }
}
就像我期待的那样

但是当我补充

date_sold.add(cols[7]);
我得到一个错误

线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:7

我不知道为什么,因为共有8列,我已从0开始编制索引。 我的while语句有问题吗?

您调用的版本会删除尾随的空字符串

因此,结果数组中不包括尾随的空字符串

您的第一行的
date\u sall
列为空。请尝试像这样调用拆分:

String[] cols = line.split(",", -1);

乍一看,我并没有发现任何错误,但我猜第二个条目
行的语句。split(“,”
并没有考虑最后一列是空的。当尝试使用Sys.out语句调试它时,请检查是哪一行造成了问题

当你用调试器检查你的代码时,你观察到了什么?非常感谢你,我可以在6分钟内按答案打勾,对不起,我的排名也不够高
String[] cols = line.split(",", -1);