[英]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.