0

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

5
  • Your error is not reproducible so it cannot get a reasonable answer. Commented Sep 22, 2014 at 12:00
  • check the length of your list "data" i.e: data.size(); before printing it's content Commented Sep 22, 2014 at 12:32
  • thanks moe , its a good idea Commented Sep 22, 2014 at 12:42
  • sorry if my answer was unclear, but that is what i meant from saying check the data has four parts first Commented Sep 22, 2014 at 12:43
  • thanks everyone now i added this line before printing array it give success if(data.length==4){ System.out.println(data[0]+" "+data[1]+" "+data[2]+" "+data[3]); } Commented Sep 22, 2014 at 16:07

3 Answers 3

2

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.

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

3 Comments

anyway thanks for your suggestion. i think it may be problem with my code.that's why i post here. No one found any error with this code , so need to check file
I thought there was an error with you not checking if there was a third index? As there may not be 3 commas in every line (as you might expect). And so check the data has a length of 4 before you ask to print them.
if this answer works/the most helpful, you could always accept it on left hand side)
1

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

4 Comments

the error occurred at line 536th line , but there is no such problem
open the file and check the line 535 and 536 there must be some difference. The data would not be available. Print the line here ... from 530 to 540 of the csv file
i copied that that line and its former line and latter upto 20. and executed but no error
better you post first 600 lines :) to find the root cause
0

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

1 Comment

thanks ram :), thanks for your co-operation i got success with moe tricks

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.