Reputation: 45
For a Java homework assignment, I need to create a class that reads and writes CSV files. I'm currently having some problems reading the the CSV. The code below, only outputs the first line of the code and then generates the following error message: 'Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at com.gc01.FileManager.CSVManager.main(CSVManager.java:27)".
I have looked at various examples, and I am aware of the 'opencsv' package, but I need to write this code myself. I have located the problem to the statement "System.out.print(data[i]);". However, when cross-referencing this code it all seems to be fine.
I am using the methods from the FileInput class, as specified by my teacher (http://www.devjavasoft.org/SecondEdition/SourceCode/Share/FileInput.java).
public class CSVManager {
public static void main(String [] args){
Scanner sc = new Scanner (System.in);
System.out.println("Please enter the file directory of the chosen CSV");
System.out.println("For Example: /Users/UserName/Downloads/FileName.csv");
///Users/ReeceAkhtar/Desktop/GeoIPCountryWhois.csv
final String fileName = sc.nextLine();
System.out.println("How many columns?");
final int columns = sc.nextInt();
BufferedReader br = null;
String line = "";
String splitBy = " , ";
try {
br = new BufferedReader(new FileReader(fileName));
while((line = br.readLine()) != null) {
for (int i = 0; i < columns; i++) {
String[] data = line.split(splitBy);
System.out.print(data[i]);
}
}
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("File Read");
}
}
Upvotes: 0
Views: 2587
Reputation: 15418
while((line = br.readLine()) != null){
for (int i = 0; i < columns; i++){
String[] data = line.split(splitBy);
System.out.print(data[i]);
}
for
loop without any reason." , "
for splitting (which might be the reason you are having ArrayIndexOfBound
exception) Instead use ","
; use trim()
on data[i]
to get rid of trailing/leading white space if you wish to.data.length
is equal to columns
for consistency.try-with-resource
which close the declared resource inside try(){}
context, allowing us to get rid of finally
block So your could should look like as follows:
try (BufferedReader br = new BufferedReader(new FileReader(fileName))){
while((line = br.readLine()) != null){
String[] data = line.split(splitBy);
if(data.length != columns)continue; // check for consistency,
//might throw an exception
for (int i = 0; i < columns; i++){
System.out.print(data[i].trim());
}
}catch(IoExection ex)
{
ex.printStackTrace();
}
Upvotes: 0
Reputation: 917
Try this one. If you take splitting out the for loop everything will be okay.
String[] data = line.split(splitBy);
while((line = br.readLine()) != null){
for (int i = 0; i < columns; i++){
System.out.print(data[i]);
}
}
Upvotes: 1
Reputation: 17622
Exception is very clear
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
means, you are trying to access 1st element in the array which doesn't exist
Since you are saying System.out.print(data[i]);
is the line where the exception is occurring, then for the first line data
must have populated with only single element
Debug the issue with IDE to find out why split method is resulting unexpected elements. I suspect usage of spaces around ,
is the cause in " , "
Upvotes: 1