Ok I am trying to finish this program for my into to java class and I am having trouble with this one part. Everything else is working except this. I have 3 arrays, 1 string, 2 type int, all parallel. I have to sort these into alphabetical order by name then by number (using the string array first, then the first int array, sorting is not affected by the third array, it is there to practice parallel in 3 dimensions)
This is the code I have so far.
public static void sortDatabase(int numRecords, String[] sDeptArr,
int[] iCourseNumArr, int[] iEnrollmentArr)
{
System.out.println("Sort the database. \n");
String sTemp = null;
int iTemp = 0;
int eTemp = 0;
for(int i=0; i<numRecords; i++)
{
int iPosMin = i;
for(int j=i+1; j<numRecords; j++)
{
String a = sDeptArr[j];
String b = sDeptArr[iPosMin];
if(a.compareTo(b) != 0)
iPosMin = j;
else if(!sDeptArr[j].equals(sDeptArr[iPosMin]) && iCourseNumArr[j] < iCourseNumArr[iPosMin])
iPosMin = j;
}
sDeptArr[i] = sTemp;
sDeptArr[i] = sDeptArr[iPosMin];
sDeptArr[iPosMin] = sTemp;
iCourseNumArr[i] = iTemp;
iCourseNumArr[i] = iCourseNumArr[iPosMin];
iCourseNumArr[iPosMin] = iTemp;
iEnrollmentArr[i] = eTemp;
iEnrollmentArr[i] = iEnrollmentArr[iPosMin];
iEnrollmentArr[iPosMin] = eTemp;
}
}
It has to be done in java. I am getting an error in the 15th line consisting of
if(a.compareTo(b) != 0)
and I am getting a NullPointerException.
Basically I am sorting fake class names and course numbers while keeping the number of people enrolled parallel with its corresponding index to the course name and number.