[英]IndexOutOfBoundsException when reading CSV file
嗨,我正在尝试读取看起来像这样的 CSV 文件->
我正在尝试阅读它并使用以下代码将其添加到数组中:
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);
我已经有一个 Producto class 并设置了它的构建器,我的程序目前工作正常,我唯一的问题是从外部来源导入数据。
当我针对“valores”的索引 0 和 1 时,它会正确打印,但是从索引 2 开始,它会给出 indexOutOfBounds 异常,我有点迷茫,我不知道为什么它超出了范围。
我已经完成了我的研究,但遗憾的是还没有弄清楚,非常感谢任何帮助。
你需要替换这个:
String datos = inputStream.next();
对此:
String datos = inputStream.nextLine();
希望对你有帮助,如果可行的话告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.