aspose file tools*
The moose likes Beginning Java and the fly likes Problem with parallel arrays when sorting in decending order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with parallel arrays when sorting in decending order" Watch "Problem with parallel arrays when sorting in decending order" New topic
Author

Problem with parallel arrays when sorting in decending order

Jim Stevens
Greenhorn

Joined: Jun 08, 2011
Posts: 28
So I have most of the program running. The goal is to have a class with array grades and names. I need to sort those in descending order. All of the tests pass except 3 and 4, which have two name and two grades. Ill post the class and the test prog. Thanks

Class



Im just wondering why it works with everything but two names.
Wim Vanni
Ranch Hand

Joined: Apr 06, 2011
Posts: 96

Welcome to the Ranch, Jim!

Could you start by editing your post and putting the code within code formatting tags? Is much more readable that way, thanks. CR beat us to it :-)

I don't see much wrong yet with your code but will investigate a little more.

Cheers
Wim
Deepakkumar Devarajan
Ranch Hand

Joined: Apr 19, 2011
Posts: 52

So you get the required output for the test 5,6, and 7,8? Please post the Tester Class too .. I'll try to sort the issue.

Note: Please enclose code modules using Code tags in future...


Regards,
Deepakkumar Devarajan
Wim Vanni
Ranch Hand

Joined: Apr 06, 2011
Posts: 96

It's your (first) for-loop. Check how many times it runs in the case of length = 2. Possibly the other tests were succesful by accident :-)

And may I suggest to always use {} brackets when you use statements like for. If you add to the code, you'll run into trouble in no time if you don't add them even if it's only 1 line of code that's in the loop.

Cheers,
Wim
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Welcome to the Ranch

Who gave you parallel arrays? That is a potential nightmare. If at all possible, change the arrays so you have one array and set up a Comparator to sort with.
You have been well-taught if you test the sizes of the arrays and throw Exceptions, but you need those tests before you try any sorting. Actually the test for empty arrays is probably unnecessary; if you pass a 0-length array, your outer for loop will look like this:

for (o = 0 - 1; o (== -1) > 1 (ie false); o--) ...

so the loop will never start, and nothing will go wrong.
I would recommend you go through the loop with a pencil and paper and see what the values are for each iteration, like that, when you are passing two elements. Then see whether you have an out-by-one error anywhere. An out-by-one will cause the loop to run once too often, or once too few times.

Also write your loops in the conventional manner
for (int o = myArray.length - 1; o > 1; o--) ...
Declare the loop indices inside the (), not before the loop.

If you can't visualise the numbers inside the loops, add some print statements inside the loops at their start. Then you can see which values of i and o are being used, but if the loop never starts, you will obviously see nothing.
Jim Stevens
Greenhorn

Joined: Jun 08, 2011
Posts: 28
Thank you for all the replies. Unfortunately i cannot change from parallel arrays. Not to sure why he wants it like this.... I went through my array a few times cannot get it going. Ill keep working any let yall know.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
I still think it might be an out-by-one error. Count carefully with a pencil and paper how many times you go through your outer loop when you pass a 2-member array.
Jim Stevens
Greenhorn

Joined: Jun 08, 2011
Posts: 28
Ok so i have my Sort class properly sorting the grades in descending order. I know what the problem is now. I need to somehow put the string names[] and int grades[] into the same index. I just am not sure of how to do that. Any ideas? Here is the new code
Jim Stevens
Greenhorn

Joined: Jun 08, 2011
Posts: 28
GREAT NEWS I have all tests passing except for the last one. Which is odd because it is a test to see if both arrays are equal...
I'll keep trying

edit: the error i am getting is an ArrayIndexOutofBoundsException


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Yes, if you are going beyond the end of an array you will suffer an ArrayIndexOutOfBoundsException. And remember a 5-element array has indices 0 1 2 3 4, not 5.

You appear to know already how to test whether two arrays are the same length; you had something in your first post. You need something like that, only at the beginning of your method.
Jim Stevens
Greenhorn

Joined: Jun 08, 2011
Posts: 28
Campbell Ritchie wrote:Yes, if you are going beyond the end of an array you will suffer an ArrayIndexOutOfBoundsException. And remember a 5-element array has indices 0 1 2 3 4, not 5.

You appear to know already how to test whether two arrays are the same length; you had something in your first post. You need something like that, only at the beginning of your method.


So are you saying i need another



before my for loop?
This is confusing me to no belief haha thanks.
Jim Stevens
Greenhorn

Joined: Jun 08, 2011
Posts: 28
GOT IT thank you very much....

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with parallel arrays when sorting in decending order
 
Similar Threads
using instance variables to sort "objects"
? about creating a method
Getting grades into a gradebook
Creating a method to average grades out
concurrency error