0
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]);
        }
    }

The program's purpose is to parse the CSV format. I have used bufferRead method.

When I go to compile the program, it works fine. When I run the program, I get an error,

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at com.testing.CsvParser.main(CsvParser.java:34)
7
  • 2
    Did you try to print the line you are attempting to split? Usually CSV files use a comma separator, not \t (CSV = Comma Separated Values). Commented Aug 4, 2015 at 10:27
  • What content you have in the file? show sample Commented Aug 4, 2015 at 10:27
  • The program is just telling you in your file, when you split it by tabulations ("\t"), there are not enough to access to the 2nd slot (tweet[1]). Check your file! Commented Aug 4, 2015 at 10:27
  • please upload sample file Commented Aug 4, 2015 at 10:40
  • 1
    See comment in your program // use comma as separator Commented Aug 4, 2015 at 10:41

1 Answer 1

2

You should use a comma separator.

Change

String cvsSplitBy = "\t";

to

String cvsSplitBy = ",";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.