• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting correctly, modifying array, then sorting incorrectly

 
Greenhorn
Posts: 20
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 20
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would probably put that array‑swapping code in a [public static] method in a utility class.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic