简体   繁体   English

读取 CSV 文件时出现 IndexOutOfBoundsException

[英]IndexOutOfBoundsException when reading CSV file

Hi i'm trying to read a CSV file which looks like this ->嗨,我正在尝试读取看起来像这样的 CSV 文件->

CSV File CSV 文件

And i'm trying to read it and add it to an array with this code:我正在尝试阅读它并使用以下代码将其添加到数组中:

ArrayListlistaProductos2 = new ArrayList(); ArrayListlistaProductos2 = new ArrayList();

    Scanner inputStream = null;
    inputStream = new Scanner(new File("src/ProductosImportados2.csv"));
    
    while(inputStream.hasNext()) {
        
        String datos = inputStream.next();
        String[] valores = datos.split(",");
        String articuloImportado = valores[0];
        String precioImportado = valores[1];
        String descripcionImportada = valores[2];
        String codigoImportado = valores[3];
        String tallaImportada = valores[4];
        String marcaImportada = valores[5];
        String colorImportado = valores[6];
        
        Producto nuevoProducto = new Producto(articuloImportado, precioImportado, descripcionImportada, codigoImportado, tallaImportada, marcaImportada, colorImportado);
        listaProductos2.add(nuevoProducto);
        System.out.println(listaProductos2);

I already have a Producto class with it's builder set up, and my program is currently working properly, my only issue goes with importing data from an outside source.我已经有一个 Producto class 并设置了它的构建器,我的程序目前工作正常,我唯一的问题是从外部来源导入数据。

When i target the index 0 and 1 of "valores", it prints correctly, but from index 2 and forward it gives the indexOutOfBounds exception, and i'm kinda lost, i don't know why it's out of bounds.当我针对“valores”的索引 0 和 1 时,它会正确打印,但是从索引 2 开始,它会给出 indexOutOfBounds 异常,我有点迷茫,我不知道为什么它超出了范围。

I've done my research, but haven't figured it out sadly, any help is greatly appreciated.我已经完成了我的研究,但遗憾的是还没有弄清楚,非常感谢任何帮助。

You need to substitute this:你需要替换这个:

String datos = inputStream.next();

In to this:对此:

String datos = inputStream.nextLine();

I hope help you, say me if it work.希望对你有帮助,如果可行的话告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM