读取 CSV 文件时出现 IndexOutOfBoundsException
IndexOutOfBoundsException when reading CSV file
您好,我正在尝试读取如下所示的 CSV 文件 ->
CSV File
我正在尝试读取它并使用以下代码将其添加到数组中:
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();
希望能帮到你,如果有用请告诉我。
您好,我正在尝试读取如下所示的 CSV 文件 ->
CSV File
我正在尝试读取它并使用以下代码将其添加到数组中:
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();
希望能帮到你,如果有用请告诉我。