Tuesday, 15 May 2012

for loop - data.put() throws IndexOutOfBoundsException -



for loop - data.put() throws IndexOutOfBoundsException -

so have code input order , cost specified number of items public class setmenu0 {

private double price; private int size; private string output; private string priceoutput; arraylist orderlist = new arraylist<string>(); arraylist pricelist = new arraylist<double>(); public setmenu0() { scanner input = new scanner(system.in); system.out.print("enter new order?(y)(n): "); string init = input.nextline(); if(init.equalsignorecase("y")) { system.out.print("enter number of orders: "); size = input.nextint(); for(int = 0; i<=size; i++) { i++; system.out.print("enter order: "); orderlist.add(input.next()); system.out.print("enter price: "); pricelist.add(input.nextdouble()); } } } public arraylist getord() { homecoming orderlist; } public arraylist getpri() { homecoming pricelist; } public int getsize() { homecoming size; }

}

now when utilize treemap indexoutofboundsexception here public static void main(string[] args) { //blank workbook xssfworkbook workbook = new xssfworkbook();

//create blank sheet xssfsheet sheet = workbook.createsheet("food"); setmenu0 set = new setmenu0(); arraylist orderlist = set.getord(); arraylist pricelist = set.getpri(); int size = orderlist.size(); map<string, object[]> info = new treemap<string, object[]>(); data.put("1", new object[]{"order", "price"}); try{ for(int i=0; i<size; i++) { int in = i+2; string linenum = ""+in; data.put(linenum, new object[]{orderlist.get(i), pricelist.get(i)}); } } catch(indexoutofboundsexception e) { system.out.print("problem"); }

the exception happens when set size 3 upwards.

your problem might in bit:

for(int = 0; i<=size; i++) { i++; system.out.print("enter order: "); orderlist.add(input.next()); system.out.print("enter price: "); pricelist.add(input.nextdouble()); }

try removing i++ loop body.

for-loop map indexoutofboundsexception treemap

No comments:

Post a Comment