This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of a two deminsional Array  RSS feed

 
Chris Hawley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Brian Mozhdehi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
}
}
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also using Exception is catch is poor practice. Use the higher level exceptions(IOException, NumberFormatException, ect).
 
Chris Hawley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your advice. Have a great day!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!