0

What I want to do is set a String called nameWithYear to be equal to be movies[0] + "(" + movies[1]+")" (So "Movie Title (Year)") from text being parsed off a CSV.

Whenever I try, I am experiencing an issue where I keep on getting an array out of bounds error. I tried setting the string equal to only movies[0], it works with success. When I try to set it to movies[1] by itself, it fails. I can do a System.out that includes element [1], and it outputs just fine with no issues. So in other words, for some reason, I can only include element[0] in the string and not element[1]. So I am assuming it has something to do with declaring the value for the string. Just not sure what.
The code is as follows:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class CSVParsing { 
public String parseCSV() {

String csvFile = "C:\\Users\\RAY\\Desktop\\movies.csv";
BufferedReader br = null;
String nameWithYear = new String();
    String line = "";
String csvSplitBy = ",";

try {

    br = new BufferedReader(new FileReader(csvFile));
    while ((line = br.readLine()) != null) {

            // use comma as separator
                    if (line.charAt(0) == '"')
                    {
                        csvSplitBy = "\",";   
                    }
                    else
                    {
                        csvSplitBy = ",";
                    }
                    String[] movies = line.split(csvSplitBy);
          nameWithYear = ""+ movies[0]+" ("+movies[1]+")";
    }

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (br != null) {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

return nameWithYear;
}

public static void main (String[] args)
{
CSVParsing obj = new CSVParsing();
String testString = obj.parseCSV();
}

}

Note that it is not 100% complete, I am testing it in small chunks to make sure it is doing everything as I want it to do.

UPDATE: Found out that it was related to blank year entries as part of the CSV. How do I handle that? The program cuts off once it finds the year entry to be blank.

UPDATE 2: I solved it with my own but of research. I am taking the results after the split() and putting them into an ArrayList. That way, I could handle blank entries in the year column by replacing them with another value.

2
  • Insert System.out.println(Arrays.toString(movies)); before the line nameWithYear = ""+ movies[0]+" ("+movies[1]+")"; and see what he prints if you run your program. The output for an array with to entries looks like this: [array with, two entries]. Commented Nov 14, 2014 at 23:13
  • Run it under debugger and inspect the values of your movie array. Commented Nov 14, 2014 at 23:15

2 Answers 2

1

I guess problem is with your input file.Make sure your input is not of the form
"cauchy hep(21\10\1991) ","cauchy hep" .Notice the space between ) and " remove the space if any since: if (line.charAt(0) == '"') { csvSplitBy = "\",";
} else { csvSplitBy = ","; }

csvSplitBy equals "\"," no space with the last character of string. If your file doesn't follow the pattern you specified whole line or whole file wil be treated as single string and will be stored in movies[0] .So there will no string at index 1 of movies that's why ArrayIndexOutOfBound. Remove the space Or you can include the space the beginning " \"," again notice the space between " \"," .It will solve the problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually, you are right about that part. It was related to the input file. I took a closer look and found that the first blank year entry was what was causing the issue. How do I handle that situation? Updated OP with that new discovery.
In my view you just have to edit the csvSplitBy part in your program it will remove the error I tried it myself.Just check once.
0

split() method returns one element in the array this means that the separator does not exist in the line variable.

line.contains(csvSplitBy);// will be evaluated to false in case of one item is returned by split();

Make sure that the CSV file is comma separated .. or the data has more than one column (CSV formatted correctly )

I hope this could help!

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.