So i have this code to input an order and a price for a 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 i = 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()
{
return orderList;
}
public ArrayList getPri()
{
return priceList;
}
public int getSize()
{
return size;
}
}
Now when i use it with the TreeMap i get an 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[]> data = 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 the size to 3 upwards.
Your problem might be in this bit:
for(int i = 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 the i++ from loop body.