File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Sorting correctly, modifying array, then sorting incorrectly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting correctly, modifying array, then sorting incorrectly" Watch "Sorting correctly, modifying array, then sorting incorrectly" New topic
Author

Sorting correctly, modifying array, then sorting incorrectly

Salma Youssef
Greenhorn

Joined: Sep 01, 2013
Posts: 20

Hi! The assignment asks me to make a program that uses Data Lists to store TV show information. It needs to be able to:
Add/enter TV shows and keep track of them
Modify TV shows by asking which show to be modified and search for it using the search key then modify the show that information
Delete TV shows (like the modify option) by searching for a show using the show's name and have it be removed by shifting the array one space
Sort TV shows by asking the user what sort key he/she would like to use (name, day, time) and sorts the list using that key (alphabetically and numerically)
Show all TV shows by outputting all the shows and also gives totals for the number of shows per day

I've written the code and it runs, but not correctly. After first inputting the show info and it sorts it properly the first time, then I delete, modify, and add more shows, it doesn't sort properly after that. It's all messed up! Can you look over my code and help me with this? I've been looking over it for hours, but I can't figure out what the problem is!!

Keep in mind that I MUST use Java 4 (required by my teacher). It's ancient and inefficient and it means that I can't use ArrayList to store my data. Can you please help me out? I don't know where I'm going wrong!
The code's very long and I apologize for it. I understand that you may not have the time to look through it, but I don't really know where the problem is coming from, so I can't highlight just one area...

Thanks,
Salma

Here's a simple class:



Here's my code:



Thanks again SO MUCH!
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
Keep in mind that I MUST use Java 4 (required by my teacher). It's ancient and inefficient and it means that I can't use ArrayList to store my data.

Err so why does your code contains the following line?


Learning to debug your code is an important part of learning to write code. The thing to do is to add print out statements in one of the sort methods and print out the values of variables so you can see what is happening as the code runs.

BTW when swapping the position of objects you don't need to copy their state to a temporary object, just copy the object reference to a temporary variable and then move the objects themselves.
Salma Youssef
Greenhorn

Joined: Sep 01, 2013
Posts: 20

Right, sorry. I meant that I can't use generics, not ArrayList. Virtual Freudian-slip. My bad!

I don't quite understand what you meant by
Tony Docherty wrote:BTW when swapping the position of objects you don't need to copy their state to a temporary object, just copy the object reference to a temporary variable and then move the objects themselves.
Could you elaborate?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
If you have an array and you want to swap the positions of two elements at say index i and index j you can just do this:

The same technique applies to a List.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
You would probably put that array‑swapping code in a [public static] method in a utility class.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sorting correctly, modifying array, then sorting incorrectly
 
Similar Threads
LinearProgramming in Java
Sorting objects in a cast in alphabetical order
Sorting with Strings and int
don't know how to call method in my new version of code
Help with writing to a file!