Reputation: 53
i have GB of size csv file , i'am able to read this but when splitting it to an array then printing cause ArrayIndexOutOfBoundsException this is my program
FileInputStream inputStream = null;
Scanner sc = null;
try {
inputStream = new FileInputStream("file.csv");
sc = new Scanner(inputStream, "UTF-8");
int j=0;
while (sc.hasNextLine()) {
String[] data=new String[4];
String line=sc.nextLine();
data=line.split(",");
System.out.println(data[0]+" "+data[1]+" "+data[2]+" "+data[3]);
}
if (sc.ioException() != null) {
throw sc.ioException();
}
}
catch (IOException ex) {
Logger.getLogger(TestPrintingAllLine.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (inputStream != null) {
inputStream.close();
}
if (sc != null) {
sc.close();
}
}
After executing 536 lines then, it caused ->
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at enterdatatosql.TestPrintingAllLine.main(TestPrintingAllLine.java:45) Java Result: 1.
45th line is-> System.out.println(data[0]+" "+data[1]+" "+data[2]+" "+data[3]);
Upvotes: 0
Views: 2201
Reputation:
I would post this as a comment, but it might get messy.
You should check you have '4 parts' on each split, as some lines may only have three/two/etc.
System.out.println(data[0]+" "+data[1]+" "+data[2]+" "+data[3]);
Just before this line, check the data
has four parts first.
EDIT
I think your problem is on line 536, you only have two commas instead of three. This means that your data array wiould look like:
|----|----|----| 0 1 2 <--indexes
where as usually you would have:
|----|----|----|----| 0 1 2 3 <--indexes
Since your print line will be looking to print the index 3
, you will get an index out of bounds error since there is no such 'part' on this line with an index of 3.
Upvotes: 2
Reputation: 107
try this code :
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
public class ReadValues {
public static void main(String[] args) throws IOException {
FileInputStream stream = new FileInputStream("D:\\jointIndustry\\Test\\src\\input.csv");
String data = IOUtils.toString(stream);
String data1[] = data.split(",");
for(String str : data1){
System.out.println(str);
}
}
}
You need to download commons-io-1.3.2.jar from maven/apache site and set it into the classapth
Upvotes: 0
Reputation: 330
Before print data[0], data[1], data[2] and data[3] check whether its null or not .. they may be the case in few rows data[1] or data[2] or data[3] will be null and it is generating this error
Upvotes: 1