I created 4 arrays to store employee-data (ID, Name, Age, Salary).
String[] empID = new String[arrayLength];
String[] empName = new String[arrayLength];
int[] empAge = new int[arrayLength];
double[] empPay = new double[arraylength];
I sorted the salary arrays in ascending order.
Arrays.sort(empPay);
I want the index of the elements in the sorted array (empPay) against the original array (tempPay) similar to the code below.
double[] price = {12,4,65.89,33.5,24,90};
double[] pr = {4,12,24,33.5,65.89,90};
int[] pos = new int[6];
int prCount = 0;
do {
for (int i=0;i<price.length;i++) {
if (pr[prCount] == price[i]) {
pos[prCount] = i;
prCount++;
}
}
} while (prCount<pr.length);
System.out.println(Arrays.toString(pos));
When I apply this logic to my code, I get an "IndexOutOfBoundException"
...
inPay = scanner.nextDouble();
for (...) {
empPay[count] = inPay;
tempPay[count] = inPay;
}
Arrays.sort(empPay);
int[] index = new int[arrayLength];
int listCount = 0;
do {
for (int j=0;j<tempPay.length;j++) {
if (empPay[listCount] == tempPay[j]) {
index[listCount] = j;
listCount++;
}
}
} while (listCount<empPay.length);
System.out.println(Arrays.toString(index));
Why does the logic work in the first code and not the second. I want to display all employee information properly aligned. Please, Kindly avoid objects or collections or other sophisticated approach to the problem. I teach Grade 9 kids.