static final int[] HotDogdb = {30, 45, 44, 37,51};
static final int[] ToastedChickendb = {25, 30, 45, 15,33};
static final int[] ToastedSteakdb = {10, 15, 12, 16,17};
static final int[] ToastedEggTdb = {14, 12, 17, 20,16};
static final int[] ToastedSteakEdb ={5, 8, 3, 8,6};
static final int[] ChickenRolldb = {27, 28, 23,20,21};
static final int[] SteakRolldb = {19, 22, 23, 21,18};
static final int[] EggTomatodb = {15, 16,10, 12,11};
static final int[] CheeseTomatodb = {18, 19, 22, 21,20};
static final int[] SteakEggdb = {10, 16, 13, 17,15};
static final int[] tCheeseTomatodb = {23, 30, 27, 40,37};
static final int[][] sales = new int[][] { HotDogdb, ToastedChickendb, ToastedSteakdb, ToastedEggTdb, ToastedSteakEdb,ChickenRolldb,SteakRolldb,EggTomatodb,CheeseTomatodb, SteakEggdb, tCheeseTomatodb };
So basically finding the sum of index @ 0 all the way through
what ive got so far
int total8 = 0;
int i;
for (i=0; i <= 11; i++){
total8 = total8 + MyConstants.sales[i][0];}
jTextArea6.setText(""+total8);
Instead of
i <= 11, usei < 11:Otherwise, you’ll get an
ArrayOutOfBoundsException, sincesales.lengthis11, and you are usingi <= 11, thus, your last iteration will try to accesssales[11]which does not exist (note that arrays in Java are 0-indexed).Another option is to use
i < sales.length. This way, if you add more elements to the array, you don’t need to change the code of yourfor-loop: