• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with this coding  RSS feed

 
Dan Czarnecki
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating a movie database program, and I would really appreciate anyone's help with a problem I'm having. The functions I'm having this program perform are adding a movie, searching for a movie by title, displaying all movies entered into the database, sorting the movies in alphabetical order, and deleting a movie from the database. All of the functions work fine except for searching for a movie and sorting them in alphabetical order.

Here is my coding for sorting them in alphabetical order, by title, using bubble sort:

Now, here is my coding for searching for a movie:


In case anyone is wondering, the IDE I'm using is BlueJ. Also, within BlueJ, I ran the debugger for both sets of code when testing it, and I noticed that the data I'm entering in is actually showing up as null for the search, therefore making the search function unusable at this time. Same thing with the sort. I know the data is being entered into the array though. When I tell my program to display all data entered into it, it shows up fine. But, I run into problems with these 2 functions. Again, any help you can give me is greatly appreciated!
 
Dan Czarnecki
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Bear Bibeault! I will now know to do that should I have anything else to post here. Hopefully, I can now get an answer quicker.
 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should use the Comparable interface for comparing the movies, and you should override the equals() method in a movie to check for equality.

Right now you're comparing object references, which will never turn out to be equal.

So, your Movie object should implement "Comparable", which makes you implement the compareTo method. And when you've done so, you can use the following method to sort it:



The Arrays.sort method will invoke the compareTo method on the movie object so it will be ordered that way.
 
Dan Czarnecki
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim Pouwels! Now my search method works perfectly. I still have one other question though. I tried putting a call for Arrays.sort into my tester class and it wouldn't compile properly. Do I need to have an import for this function?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Czarnecki wrote:Thanks Jim Pouwels! Now my search method works perfectly. I still have one other question though. I tried putting a call for Arrays.sort into my tester class and it wouldn't compile properly. Do I need to have an import for this function?

Well, you probably need an import for the Arrays class itself. My question is: why use arrays at all? Java has a pile of nice collections classes that will arrange your movies any way you like (and some of them will eliminate duplicates as well).

The closest one to an array is an ArrayList (which can also be sorted if you want).

Winston
 
Dan Czarnecki
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take back that last post I put up here. I just recently put my bubble sort coding back in, and it works perfectly, for the most part. The only other problem I'm running into now is that after I have it call the bubbleSort() method, it also calls the deleteMovie() method, which I do not have any call to in my bubbleSort() method. Any idea what's going on?
 
Dan Czarnecki
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of the help everyone! My last fix was something I overlooked. I simply just forgot to put a break in case 4 of my switch statement. My program is now 100% working!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!