java 中的数组索引越界

Array Index Out of Bound in java

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)

您应该使用逗号分隔符。

改变

String cvsSplitBy = "\t";

String cvsSplitBy = ",";