"main" java.lang.ArrayIndexOutOfBoundsException: 3



我想提取CSV的特定单列值,file.so 我使用了具有实例:45211和属性数:17的CSV FILE数据集。我尝试过使用这段代码..但它给了我这样的错误.."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)文件中可以有空的"列"吗?如果有,请使用以下命令进行拆分:

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

相关内容

  • 没有找到相关文章

最新更新