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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Jeanne Boyarsky
• Devaka Cooray
• Paul Clapham
Sheriffs:
• Tim Cooke
• Knute Snortum
• Bear Bibeault
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Ganesh Patekar
Bartenders:
• Frits Walraven
• Carey Brown
• Tim Holloway

# Use of a two deminsional Array

Greenhorn
Posts: 2
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
Spiderman 2 - 4
Finding Nemo - 3
Star Wars II - 1

Chris

author
Posts: 23834
140
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

Ranch Hand
Posts: 81
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;
}
}
}

Ranch Hand
Posts: 1970

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.

Ranch Hand
Posts: 490
Also using Exception is catch is poor practice. Use the higher level exceptions(IOException, NumberFormatException, ect).

Chris Hawley
Greenhorn
Posts: 2