Forums Register Login

Use of a two deminsional Array

+Pie Number of slices to send: Send
All,

I am new to Java and I need to write a program which utilizes a class named video w/ two variables and a applications class named videostore which will sort 5 movies by title and corresponding Ratings. I have so far been able to get the movies sorted by title and displayed in an unsorted state and in alphabetical order. But have not been able to figure out how to implement a two deminisional Array to enable me to sort by Ratings. I was also instructed to use BubbleSort, however I could not get any whare close to a successfull complile so, as you'll notice I have utilized Array.sort for the code following. Any help would be greatly appreciated.

The five movies w/ ratings are below:

Meet the Fockers - 5
Blade - 3
Spiderman 2 - 4
Finding Nemo - 3
Star Wars II - 1



Thanks in Advance for your assistance!

Chris
+Pie Number of slices to send: Send
You have to fix the sort problem first. The array.sort() method uses the quick sort algorithm. And even if it did do the bubble sort algorithm, I am pretty sure your professor wouldn't allow you to use it, as it defeats the purpose of the assignment.

Henry
+Pie Number of slices to send: Send
First, you do not need a two dimensional Array to solve your issue. Thats making the issue way more complicated than it needs to be.

Below is the syntax of a bubble sort and your titles sorted by ratings. To do the titles sorted by alpha, eliminate the references to the ratings in the same code below (because you dont need them for that part of it) and use an appropriate String comparison instead of the int one below (hint search on compareToIgnoreCase online).

for(int ctr1 = 0; ctr1 < rating.length - 1; ctr1++)
{
for(int ctr2 = ctr1 + 1; ctr2 < rating.length; ctr2++)
{
int one = rating[ctr1];
int two = rating[ctr2];

String title1 = title[ctr1];
String title2 = title[ctr2];

if(one > two)
{

title[ctr1] = title2;
title[ctr2] = title1;
}
}
}
+Pie Number of slices to send: Send
While we're here, to help you with some other aspects of your program...

Originally posted by Chris Hawley:



Catching and silently squashing all Exceptions is close to the Number One Sin in Java. Exceptions are not just there to annoy you, they indicate a problem that needs handling.

You need to consider the "expected" Exceptions, if any, that could occur due to user error, system problems etc, and handle them.

Any other Exceptions that occur would probably indicate bugs in the program. It is important that you see these, so you can fix the bugs. In a simple program like yours, it is best not to handle such "unexpected" Exceptions at all; the system will automatically exit the program and helpfully print a stack trace that will help you to see what is wrong.

Originally posted by Chris Hawley:



There is no need to call System.exit(). You program will exit just fine when it gets to the end. In a simple application like yours, calling System.exit() doesn't do any harm, but you should not get into the habit of it, because it can cause problems when you move on to writing programs with more than one thread. Calling System.exit() is something you should only do when you have a program that really cannot cleanly exit any other way.
+Pie Number of slices to send: Send
Also using Exception is catch is poor practice. Use the higher level exceptions(IOException, NumberFormatException, ect).
+Pie Number of slices to send: Send
Thank you all for your advice. Have a great day!
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 951 times.
Similar Threads
Descending order and associating Array Elements in sort method to another's elements
null pointer
multidimensional arrays
I can NOT compile first class below
question on array and printing
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:31:09.