"main" java.lang.ArrayIndexOutOfBoundsException: 3

"main" java.lang.ArrayIndexOutOfBoundsException: 3

我想提取 CSV 的特定单列值 file.so 我使用了具有 Instances:45211 和数量 Attributes:17 的 CSV 文件数据集。 我试过这段代码..但它给我这样的错误.. "main" java.lang.ArrayIndexOutOfBoundsException: 3

请帮助我...

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

        }

    }

我假设您想一次阅读一行:

    Scanner inputStream = new Scanner(file);

    while(inputStream.hasNextLine())
    {
        String data = inputStream.nextLine();
        String[] values = data.split(",");
        System.out.println(values[3]); // now, this line is safe only if you are 
                                       // sure that each row would have at least 3 commas
    }  

(1)您确定文件中的分隔符是逗号吗?

(2) 文件里能不能有空的"columns"?如果有,使用以下进行拆分:

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