import javax.swing.JOptionPane; public class NetEarnings { public static void main (String args[]) { int N = Integer.parseInt (JOptionPane.showInputDialog("Enter number of Salespersons : ")); double Salesp[][] = new double[N][5]; for (int i = 0; i < Salesp.length; i++) { String sales = JOptionPane.showInputDialog ("Start entering sales amount for Salesperson #" +(i + 1)+ " : "); String quota = JOptionPane.showInputDialog ("Start entering the quota for Salesperson #" +(i + 1)+ " : "); for (int j = 0; j < (Salesp[i].length) - 3; j++) { Salesp[i][j] = Integer.parseInt(sales); Salesp[i][j + 1] = Integer.parseInt(quota); if (Salesp[i][j] >= (Salesp[i][j + 1] + (Salesp[i][j + 1] * 0.2))) { //if bigger than quota by 20% Salesp[i][j + 3] = Salesp[i][j] + 200; //add 200 to TotalEarnings if (Salesp[i][j + 3] > 2000) { Salesp[i][j + 2] = Salesp[i][j] * 0.05; //calculate tax Salesp[i][j + 4] = Salesp[i][j] - Salesp[i][j + 2]; //put remainder after tax, in NetEarnings } } else if (Salesp[i][j] == Salesp[i][j + 1]) { //if equal to quota Salesp[i][j + 3] = Salesp[i][j] + (Salesp[i][j] * 0.06); //6% commission rate added if (Salesp[i][j + 3] > 1500) { Salesp[i][j + 2] = Salesp[i][j] * 0.04; Salesp[i][j + 4] = Salesp[i][j] - Salesp[i][j + 2]; //put remainder after tax, in NetEarnings } } else if (Salesp[i][j] == (Salesp[i][j + 1] * 0.8)) { //if 80% of quota Salesp[i][j + 3] = Salesp[i][j] + (Salesp[i][j] * 0.05); //5% commission rate added if (Salesp[i][j + 3] < 1500) { Salesp[i][j + 2] = Salesp[i][j] * 0.03; Salesp[i][j + 4] = Salesp[i][j] - Salesp[i][j + 2]; //put remainder after tax, in NetEarnings } } else Salesp[i][j + 3] = Salesp[i][j + 4] = Salesp[i][j]; } } String s = ""; for (int i = 0; i < Salesp.length; i++) { for (int j = 0; i < Salesp[i].length; j++) { s += "The taxes of Salesperson #" +(i + 1)+ " are : "+ Salesp[i][j + 2] +"."+ " Their Total Earnings are : " +Salesp[i][j + 3] + "."+ " And their Net Earnings are : " +Salesp[i][j + 4]+ "."; } } JOptionPane.showMessageDialog (null,s); } }

# Array out of bounds? Really lost here...

Page 1 of 1## 2 Replies - 735 Views - Last Post: 28 October 2010 - 01:04 PM

### #1

# Array out of bounds? Really lost here...

Posted 28 October 2010 - 12:17 PM

Hello, I have the following predicament..A 2d array which has 3 employees let's say, in each employee, there is an array of 5 elements, Sales,Quota,Taxes,TotalEarnings and NetEarnings. a lot of exceptions are required, and are implemented and explained in comments. but I am always getting "Array out of bounds on line X", every line that gives this error, has [j + 4].. isn't j + 4 supposed to mean the fifth cell? I'm really desperate here, thank you very much in advance!

##
**Replies To:** Array out of bounds? Really lost here...

### #2

## Re: Array out of bounds? Really lost here...

Posted 28 October 2010 - 12:51 PM

for (int j = 0; j < (Salesp[i].length) - 3; j++)

So in your case of 5 elements, j goes from 0 up to but not including (5 - 3), in other words, from 0 to 1.

Then you have this:

Salesp[i][j + 4] = Salesp[i][j] - Salesp[i][j + 2];

so second time round the loop, j = 1, and j + 4 = 5. Since the array is zero-indexed, it contains 5 elements numbered 0, 1, 2, 3 and 4.

5 > 4, hence array index out of bounds.

I think your logic is wonky when building that for loop in the first place, though.

### #3

## Re: Array out of bounds? Really lost here...

Posted 28 October 2010 - 01:04 PM

The problem is the display loop. You don't offset into the second dimension of the array but use it's contents directly:

The same would be true for the earlier loops. The second dimension has a fixed size so you want to directly index it.

for (int i = 0; i < Salesp.length; i++) { s += "The taxes of Salesperson #" + (i + 1) + " are : " + Salesp[i][2] + "." + " Their Total Earnings are : " + Salesp[i][3] + "." + " And their Net Earnings are : " + Salesp[i][4] + ".\n"; }

The same would be true for the earlier loops. The second dimension has a fixed size so you want to directly index it.

This post has been edited by **n8wxs**: 28 October 2010 - 01:06 PM

Page 1 of 1