java - Out of bounds array into multidimentional array -
this question has answer here:
i'm trying program store 1 dimentional array 2 dimentions because have product id's , amount of units sold. it's telling me array out of bounds in loop. tried many ways can't single dimentional array go multidimentional array. spent 2 days trying figure out this. please me! , yes have right imports, i'm not including them in here.
class productcalculator { public static void main(string[] args) { string csvfile = "/users/garre_000/desktop/comp 182/sales data.csv"; bufferedreader br = null; string line = ""; string cvssplitby = ","; try { br = new bufferedreader(new filereader(csvfile)); while ((line = br.readline()) != null) { string[] productsales = line.split(cvssplitby); system.out.println(productsales[0] + " " + productsales[1]); //system.out.println(productsales[0][0] + productsales[0][1]); (int = 2; <= productsales.length; i+=2) { int x = integer.parseint(productsales[i]); int z = integer.parseint(productsales[i++]); int[][] multiproduct = new int[x][z]; system.out.println(multiproduct[0][0]); } } } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { if (br != null) { try { br.close(); } catch (ioexception e) { e.printstacktrace(); } } } system.out.println("done"); } }
for (int = 2; <= productsales.length; i+=2) { int x = integer.parseint(productsales[i]); int z = integer.parseint(productsales[i++]);
you need able read @ least 2 elements productsales
in loop body here - however, current loop guard, run body , including i == productsales.length
, try read:
productsales[productsales.length] // 1 beyond end of array productsales[productsales.length + 1] // 2 beyond end of array
hence, need loop guard be:
for (int = 2; < productsales.length - 1; += 2) {
Comments
Post a Comment