Search code examples
javacsvstreamfilestreamopencsv

"main" java.lang.ArrayIndexOutOfBoundsException: 3


i want to extract a particular single column value of CSV file.so i had used dataset of CSV FILE having Instances:45211 and Number of Attributes:17. i had try with this code ..but its give me error like this.. "main" java.lang.ArrayIndexOutOfBoundsException: 3

pls help me...

 import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class Main {

        public static void main(String[] args) {
            // TODO code application logic here

            String filename ="bank-full.csv";
            File file= new File(filename);

            try {

                Scanner inputStream = new Scanner(file);
                inputStream.next();
                 while(inputStream.hasNext())
                {
                    String data= inputStream.next();
                    String[] values = data.split(",");
                   // double balance= Double.parseDouble(values[2]);
                    System.out.println(values[3]);
                }           

                    inputStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

Solution

  • (1) Are you sure the delimeter in the file is a comma?

    (2) Can there be empty "columns" in the file? If there are, use the following to do the split:

    String[] values = data.split(",", -1);