0

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.

3
  • Why are you using three separate arrays instead of encapsulating all that data in one object? Commented Nov 29, 2010 at 21:42
  • @matt b: If the paper requires you to implement the solution without using objects, then it's either a ham-fisted leadup to showing you why objects are useful, or the course sucks. Commented Nov 29, 2010 at 21:46
  • Its the programming assignment leading to object oriented. Commented Nov 29, 2010 at 21:48

4 Answers 4

1

After your second loop (the for (int j=... one), you have the following code:

       sDeptArr[i] = sTemp;
       sDeptArr[i] = sDeptArr[iPosMin];
       sDeptArr[iPosMin] = sTemp;

From perusing the code, my understanding is that you are trying to swap the elements in sDeptArr[i] and sDeptArr[iPosMin], however, your assignments are incorrect.

Given that it's homework, I think you should try to work out why this code won't work, and gives you the result that it does. You could step through the code in a debugger, or (given that it's fairly straightforward) step through the code in your head.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for finding the real problem. Hint: This problem is causing the other problem. Fix this first and try again.
1

This may happen only if a==null. But a is sDeptArr[j]. It means that this element of array is null.

So, data that you are sending to this method is inconsistent.

But why are you implementing sorting this way? The "right" way to solve this problem is to create new data structure that contains as many fields as you need. Then create one array and sort it using Arrays.sort(). The data structure I mentioned should implement Comparable.

Even if this is an exercise for sorting, create the data structure, one array and write sorting algorithm.

And yet another note. It seems that you have a C background because your method accepts array(s) and the length as separate parameter. Java arrays "know" their length. Use a.length to retrieve the length of array a.

1 Comment

I'd guess that the array's size doesn't necessarily reflect the number of records in that array, hence the counter variable.
0

I am getting an error in the 15th line consisting of

if(a.compareTo(b) != 0)

and I am getting a NullPointerException.

This indicates that one of the elements in your array is null. Check the contents of the array to make sure that what you are trying to sort does not contain null elements. Make sure you aren't forgetting that array indices start at 0. If null is a legit value for your array to have pre-sorting, then make sure your code guards against it.

Comments

0

Assuming you need to stay at array level (and not create higher level data structures, which in the real world you should) and use a bubble sort (which you seem to be basically using, but in the real world you often shouldn't, too slow)...

You're getting yourself tied up with too many temp positions/variables. All you really need to do loop outer for a max of the whole length, and inner for each non-examined index (see bubble sort), then compare "i" to "i+1" based on your criteria (which consider two values at most in this case) and "swap" them if they're backwards.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.