# Thread: Help with 2d array sorting problem

1. Member
Join Date
Aug 2011
Location
West Virginia
Posts
38
Rep Power
0

## Help with 2d array sorting problem

Hello All! I am currently working on a program for my programming class. The instructions are to create executable functions outside of the main (we cannot edit the main at all) that will first calculate the employee's salary based on their "employee code" and then after that, sort the array based on order of salary. I think I have a lot of the necessary pieces, but every time I compile it keeps coming back

C:\Users\broo7198\CISC230\PayrollReportSorting.jav a:120: missing return statement
}
^
1 error

Tool completed with exit code 1

here is my code thus far:

Java Code:
```class PayrollReportSorting {
public static void main(String args[]) {
// First two columns are employee id and type:
// 1 - Salaried employee with yearly salary
// 2 - Hourly employee with pay rate and number of hours worked
// 3 - Contractor with how much was earned in a year with up to
//     three contracts
double[][] employees = {{101, 1, 50000.00,     0.00,    0.00},
{102, 2,    25.50,  2000.00,    0.00},
{103, 3, 70000.00,     0.00,    0.00},
{104, 3,  5500.00, 38000.00, 2400.00},
{105, 2,    15.75,  1573.25,    0.00},
{106, 1, 75000.00,     0.00,    0.00},
{107, 2,    50.00,   750.00,    0.00},
{108, 1, 30000.00,     0.00,    0.00},
{109, 3, 10000.00, 10000.00, 3000.00}};

printEmployees(employees);
sortEmployees(employees);
printEmployees(employees);
}
static void sortEmployees(double[][] twoDimensionalArray){

int counter = 0;

System.out.println("Calculating Salaries");

while (counter <= twoDimensionalArray[0].length) {

if(twoDimensionalArray[counter][2] == 1) {

twoDimensionalArray[counter][6] = twoDimensionalArray[counter][3];

counter=counter+1;
}
else if(twoDimensionalArray[counter][2] == 2) {

twoDimensionalArray[counter][6] = (twoDimensionalArray[counter][3] * twoDimensionalArray[counter][4]);

counter=counter+1;
}
else {

twoDimensionalArray[counter][6] = (twoDimensionalArray[counter][3] + twoDimensionalArray[counter][4] + twoDimensionalArray[counter][5]);

counter=counter+1;
}

}

//now sort based on newly added values to array

int rows = twoDimensionalArray.length;

boolean isEndReached = false;

int secondcounter = 0;
double [] temp;

for(int i = 0; i < rows; i++) {

while(!isEndReached){

if(twoDimensionalArray[secondcounter][6] < twoDimensionalArray[secondcounter+1][6]) {

temp = twoDimensionalArray[secondcounter];
twoDimensionalArray[secondcounter] = twoDimensionalArray[secondcounter+1];
twoDimensionalArray[secondcounter+1] = temp;

}

secondcounter = secondcounter+1;
if(secondcounter >= rows+1){
isEndReached = true;
}
}
}

}

// This is just an example function/method to show you another way to iterate
// through an array
static void printEmployees(double[][] twoDimensionalArray) {
System.out.println(" ID " + "\t" + "Type" + "\t" + "\$" + "\t" + "\$" + "\t" + "\$");
System.out.println();
for (int row=0; row < twoDimensionalArray.length; row=row+1) {
for (int column=0; column < twoDimensionalArray[row].length; column=column+1) {
System.out.print(twoDimensionalArray[row][column] + "\t");
}
System.out.println();
}
System.out.println();
}
}```

Thanks for any advice/help. I apologize if there are any glaringly obvious mistakes, Im a rookie.
Last edited by broo7198; 09-30-2011 at 06:40 PM. Reason: changed sortEmployees function from return type Double to return type Void

2. Member
Join Date
Sep 2011
Posts
20
Rep Power
0

## Re: Help with 2d array sorting problem

In the method "static double sortEmployees(double[][] twoDimensionalArray)" you need to RETURN an double value.
If you dont want to return any value, then make a method "static void sortEmployees(double[][] twoDimensionalArray)".

bye!

3. Member
Join Date
Aug 2011
Location
West Virginia
Posts
38
Rep Power
0

## Re: Help with 2d array sorting problem

ah... that makes more sense! Thanks DIego... but now everything compiles beautifully and I get a runtime error:

"Exception in thread "main" java.lang.ArrayIndexOutofBoundsException: 5
at PayrollReportSorting.sortEmployees(PayrollReportSo rting.java:84)
at PayrollReportSorting.main(PayrollReportSorting.jav a.54)

4. ## Re: Help with 2d array sorting problem

ArrayIndexOutofBoundsException: 5
at PayrollReportSorting.sortEmployees(PayrollReportSo rting.java:84)
At line 84 the index to an array went out of bounds.
Remember array indexes start at 0.

Look at that line and see which index is past the end of the array.
Use printlns to print the index values if you can not see which one is going past the end of the array.
Last edited by Norm; 09-30-2011 at 06:54 PM.

5. Member
Join Date
Sep 2011
Posts
20
Rep Power
0

## Re: Help with 2d array sorting problem

If "double[][] employees" is a matrix 9x5, you cannot try to access to a cell in the column 5 (or more). Remember than any array start it indexes in 0.

You columns are: 0,1,2,3,4 And your Rows are: 0,1,2,...,7,8

Sorry for my poor english!

6. Member
Join Date
Aug 2011
Location
West Virginia
Posts
38
Rep Power
0

## Re: Help with 2d array sorting problem

Okay, so I realized after a while that I would not be able to "add" additional slots in to the array after it is initialized, so I went a different route. Here is my new and updated code, it fully compiles, and when I add /* */ over the "sortEmployees()" function, the entire program runs fine, printing out the array twice. Obviously the problem lies in the sorting algorithm. I get a runtime error "outofbounds" exceptions at lines 9, 13, 74, and 54.

Java Code:
```class PayrollReportSorting1 {
public static void main(String args[]) {
// First two columns are employee id and type:
// 1 - Salaried employee with yearly salary
// 2 - Hourly employee with pay rate and number of hours worked
// 3 - Contractor with how much was earned in a year with up to
//     three contracts
double[][] employees = {{101, 1, 50000.00,     0.00,    0.00},
{102, 2,    25.50,  2000.00,    0.00},
{103, 3, 70000.00,     0.00,    0.00},
{104, 3,  5500.00, 38000.00, 2400.00},
{105, 2,    15.75,  1573.25,    0.00},
{106, 1, 75000.00,     0.00,    0.00},
{107, 2,    50.00,   750.00,    0.00},
{108, 1, 30000.00,     0.00,    0.00},
{109, 3, 10000.00, 10000.00, 3000.00}};

printEmployees(employees);
sortEmployees(employees);
printEmployees(employees);
}
static void sortEmployees(double[][] twoDimensionalArray){
int row = twoDimensionalArray[0].length;
int etype1 = 0;
int etype2 = 0;
double salary1 = 0;
double salary2 = 0;
int rows = twoDimensionalArray.length-1;
boolean isEndReached = false;
double [] temp;

//now sort the array

for(int i = 0; i < rows; i++) {

while(!isEndReached){

etype1 = checkEmployeeType(twoDimensionalArray, row);
etype2 = checkEmployeeType(twoDimensionalArray, row+1);

salary1 = calculateSalary(twoDimensionalArray, etype1, row);
salary2 = calculateSalary(twoDimensionalArray, etype2, row+1);

if(salary1 < salary2) {

temp = twoDimensionalArray[row];
twoDimensionalArray[row] = twoDimensionalArray[row+1];
twoDimensionalArray[row+1] = temp;

}

row = row+1;
if(i >= rows+1){
isEndReached = true;
}
}
}

}

// This is just an example function/method to show you another way to iterate
// through an array
static void printEmployees(double[][] twoDimensionalArray) {

double salary = 0;

int type;

System.out.println(" ID " + "\t" + "Type" + "\t" + "\$" + "\t" + "\$" + "\t" + "\$"+"\t\t" + "Total Salary");
System.out.println();
for (int row=0; row < twoDimensionalArray.length; row=row+1) {

type = checkEmployeeType(twoDimensionalArray, row); // determine employee type and return to print function
salary = calculateSalary(twoDimensionalArray, type, row); // calculate yearly salaries and return to print function

for (int column=0; column < twoDimensionalArray[row].length; column=column+1) {

if(twoDimensionalArray[row][column] == 0) {
System.out.print("\t");
}
else
System.out.print(twoDimensionalArray[row][column] + "\t");
}
System.out.print("\t"+ "\$"+salary);
System.out.println();
}
System.out.println();
}

static int checkEmployeeType(double[][] twoDimensionalArray, int row){ // this function determines the employee type, then returns to the main
if(twoDimensionalArray[row][1] == 1) {

return 1;
}
else if(twoDimensionalArray[row][1] == 2) {

return 2;
}
else {
return 3;
}
}

static double calculateSalary(double[][] twoDimensionalArray, int type, int row) {  // the calculateSalary function takes the employee type and determines the yearly salary from that, then returns it

if (type==1){

return twoDimensionalArray[row][2];

}
if (type ==2) {

return (twoDimensionalArray[row][2]*twoDimensionalArray[row][3]);

}

else  {
return (twoDimensionalArray[row][2]+twoDimensionalArray[row][3]+twoDimensionalArray[row][4]);
}

}

}```

7. ## Re: Help with 2d array sorting problem

I get a runtime error "outofbounds" exceptions at lines 9, 13, 74, and 54.
Have you looked at those lines to see why the index goes out of bounds?

None of those line numbers match anything in your posted code.

When you get errors, please copy and paste the the FULL text of the error messages. Do not edit out anything.

8. Member
Join Date
Aug 2011
Location
West Virginia
Posts
38
Rep Power
0

## Re: Help with 2d array sorting problem

Originally Posted by Norm
Have you looked at those lines to see why the index goes out of bounds?

None of those line numbers match anything in your posted code.

When you get errors, please copy and paste the the FULL text of the error messages. Do not edit out anything.
Sorry, I don't know how to copy/paste out of the command window and I am shorthanding. Here's the full error:

Exception in thread "main" java.lang.ArrayIndexOutofBoundsException: 9
at PayrollReportSorting1.checkEmployeeType(PayrollRep ortSorting1.java: 136)
at PayrollReportSorting1.sortEmployees(PayrollReportS orting1.java: 74)
at PayrollReportSorting1.main(PayrollReportSorting1.j ava: 54)

9. ## Re: Help with 2d array sorting problem

Exception in thread "main" java.lang.ArrayIndexOutofBoundsException: 9
at PayrollReportSorting1.checkEmployeeType(PayrollReportSorting1.java: 136)
at PayrollReportSorting1.sortEmployees(PayrollReportS orting1.java: 74)
at PayrollReportSorting1.main(PayrollReportSorting1.j ava: 54)
The error occured at line 136 in the checkEmployeeType method that was called from line 74.

The index value was 9. What is the size of the array? What is the max valid index value for that array?

Here is how to copy the command prompt window:

To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.
Last edited by Norm; 09-30-2011 at 10:17 PM. Reason: Added instructions for command prompt window

10. Member
Join Date
Aug 2011
Location
West Virginia
Posts
38
Rep Power
0

## Re: Help with 2d array sorting problem

thanks to all of your (yet again) wonderful contributions I have solved the riddle. Here's the final code:

Java Code:
```class PayrollReportSorting1 {
public static void main(String args[]) {
// First two columns are employee id and type:
// 1 - Salaried employee with yearly salary
// 2 - Hourly employee with pay rate and number of hours worked
// 3 - Contractor with how much was earned in a year with up to
//     three contracts
double[][] employees = {{101, 1, 50000.00,     0.00,    0.00},
{102, 2,    25.50,  2000.00,    0.00},
{103, 3, 70000.00,     0.00,    0.00},
{104, 3,  5500.00, 38000.00, 2400.00},
{105, 2,    15.75,  1573.25,    0.00},
{106, 1, 75000.00,     0.00,    0.00},
{107, 2,    50.00,   750.00,    0.00},
{108, 1, 30000.00,     0.00,    0.00},
{109, 3, 10000.00, 10000.00, 3000.00}};

printEmployees(employees);
sortEmployees(employees);
printEmployees(employees);
}
static void sortEmployees(double[][] twoDimensionalArray){

int etype1 = 0;
int etype2 = 0;
double salary1 = 0;
double salary2 = 0;
int rows = twoDimensionalArray.length;
boolean isEndReached = false;
double [] temp;

//now sort based on newly added values to array

for(int j = 0; j < (rows); j++) {

int i  = 0;
isEndReached = false;

while(!isEndReached){

etype1 = checkEmployeeType(twoDimensionalArray, i);
etype2 = checkEmployeeType(twoDimensionalArray, (i+1));

salary1 = calculateSalary(twoDimensionalArray, etype1, i);
salary2 = calculateSalary(twoDimensionalArray, etype2, (i+1));

if(salary1 < salary2) {

temp = twoDimensionalArray[i];
twoDimensionalArray[i] = twoDimensionalArray[(i+1)];
twoDimensionalArray[(i+1)] = temp;

}

i = (i+1);

if(i >= rows-1){
isEndReached = true;
}
}
}

}

// This is just an example function/method to show you another way to iterate
// through an array
static void printEmployees(double[][] twoDimensionalArray) {

double salary = 0;

int type;

System.out.println(" ID " + "\t" + "Type" + "\t" + "\$" + "\t" + "\$" + "\t" + "\$"+"\t\t" + "Total Salary");
System.out.println();
for (int row=0; row < twoDimensionalArray.length; row=row+1) {

type = checkEmployeeType(twoDimensionalArray, row); // determine employee type and return to print function
salary = calculateSalary(twoDimensionalArray, type, row); // calculate yearly salaries and return to print function

for (int column=0; column < twoDimensionalArray[row].length; column=column+1) {

if(twoDimensionalArray[row][column] == 0) {
System.out.print("\t");
}
else
System.out.print(twoDimensionalArray[row][column] + "\t");
}
System.out.print("\t"+ "\$"+salary);
System.out.println();
}
System.out.println();
}

static int checkEmployeeType(double[][] twoDimensionalArray, int row){ // this function determines the employee type, then returns to the main
if(twoDimensionalArray[row][1] == 1) {

return 1;
}
else if(twoDimensionalArray[row][1] == 2) {

return 2;
}
else {
return 3;
}
}

static double calculateSalary(double[][] twoDimensionalArray, int type, int row) {  // the calculateSalary function takes the employee type and determines the yearly salary from that, then returns it

if (type==1){

return twoDimensionalArray[row][2];

}
if (type ==2) {

return (twoDimensionalArray[row][2]*twoDimensionalArray[row][3]);

}

else  {
return (twoDimensionalArray[row][2]+twoDimensionalArray[row][3]+twoDimensionalArray[row][4]);
}

}

}```
There's no feeling like banging your head against the wall for hours to eventually find the resolution!!

- DB

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•