Results 1 to 10 of 10
- 09-30-2011, 06:22 PM #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
- 09-30-2011, 06:30 PM #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!
- 09-30-2011, 06:34 PM #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)
- 09-30-2011, 06:52 PM #4
Re: Help with 2d array sorting problem
At line 84 the index to an array went out of bounds.ArrayIndexOutofBoundsException: 5
at PayrollReportSorting.sortEmployees(PayrollReportSo rting.java:84)
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.
- 09-30-2011, 06:53 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!
- 09-30-2011, 09:56 PM #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]); } } }
- 09-30-2011, 10:01 PM #7
Re: Help with 2d array sorting problem
Have you looked at those lines to see why the index goes out of bounds?I get a runtime error "outofbounds" exceptions at lines 9, 13, 74, and 54.
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.
- 09-30-2011, 10:06 PM #8
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
Re: Help with 2d array sorting problem
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)
- 09-30-2011, 10:10 PM #9
Re: Help with 2d array sorting problem
The error occured at line 136 in the checkEmployeeType method that was called from line 74.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 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
- 09-30-2011, 10:39 PM #10
Member
- Join Date
- Aug 2011
- Location
- West Virginia
- Posts
- 38
- Rep Power
- 0
Re: Help with 2d array sorting problem
Alright ladies and gents,
thanks to all of your (yet again) wonderful contributions I have solved the riddle. Here's the final code:
There's no feeling like banging your head against the wall for hours to eventually find the resolution!!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]); } } }
- DB
Similar Threads
-
Sorting Array Issue
By larson1118 in forum New To JavaReplies: 2Last Post: 04-21-2011, 05:31 PM -
Sorting Array UI
By Brandon Seale in forum New To JavaReplies: 6Last Post: 02-18-2011, 01:50 AM -
Array List Sorting
By makpandian in forum New To JavaReplies: 5Last Post: 11-14-2010, 02:33 AM -
Sorting Array
By saqib15 in forum New To JavaReplies: 1Last Post: 02-12-2010, 03:42 AM -
Sorting an array of Strings
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:39 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks