数组索引在Java中超出范围

时间:2015-08-04 10:23:56

标签: java arrays arraylist file-handling

package com.testing;

import java.io.BufferedReader;
import java.io.FileReader;

public class CsvParser {
    public static void main(String[] args) {
        String csvFile = "D:/code-home/SentimentAnalysis/test_data/Sentiment Analysis Dataset.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = "\t";

        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {
                            // use comma as separator
            String []tweet = line.split(cvsSplitBy);
            System.out.println(tweet[1]);
            System.out.println(tweet[3]);
        }
    }

该程序的目的是解析CSV格式。我使用了bufferRead方法。

当我去编译程序时,它工作正常。当我运行程序时,我收到错误,

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at com.testing.CsvParser.main(CsvParser.java:34)

1 个答案:

答案 0 :(得分:2)

您应该使用逗号分隔符。

更改

String cvsSplitBy = "\t";

String cvsSplitBy = ",";