• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting Arrays

 
Chris Rothgeb
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having much difficulty getting this program to sort the DVD titles. Could someone please point me in the right direction or show me where or what I am doing wrong. The code will not compile due to errors related to the sorting code. I have read a bunch of information and am not sure where to go from here.


 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris,

I see five errors that are simple syntax issues. You should follow what the compiler tells you the errors are.

As for the error on Collections.sort(List);, the API for Collections.sort(List<T>) explains what is required of the elements of the list in order for the list to be sorted via this method. DVD doesn't meet these requirements.
[ May 09, 2008: Message edited by: Stevi Deter ]
 
Chris Rothgeb
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have reworked this program several times with different sorting methods. I cannot get this thing to compile nor can I see where the problem is.

Would someone please tell me what the problem is with this program?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stevi Deter:
You should follow what the compiler tells you the errors are.

Start at the first compiler error and work from there.
 
Chris Rothgeb
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler tells me that it cannot find the symbols for line 15 and 16 or class DVDProducts which is:
String title1 = products[i].gettitle();
String title2 = products[j].gettitle();

I don't have a clue as to where else there should be a symbol in the code. I have checked this against a number of examples and I am baffled.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that the gettitle method exists? Hint: Java is case sensitive.
 
arulk pillai
Author
Ranch Hand
Posts: 3388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using any IDE like eclipse or just using a text pad?
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After you finish getting all the bugs out and compiling your program, here is some more advice:

I see you used encapsulization (making your class variables private and get and set methods public) but in the case of your program I think you are using encapsulizing several times where you do not need to. For example, you have this code:

public void setTitle(String title) {
this.title = title;
}// end setTitle
public String getTitle() {
return title;
}//end getTitle

Ask yourself, what is the difference between these two statements?
myProduct.title = "Killer Martians";
myProduct.setTitle("Killer Martians");

When I encapsulize, I do it in order to protect my class variables from being set to something I don't want them to be set to, for example:


class DividingTime{
private int divideByThisInt = 1;

public void setTheInt(int newInt){
if (newInt != 0){
divideByThisInt = newInt;
}
}

public int theAnswer(anInt){
return anInt / divideByThisInt;
}

}


With this class, divideByThisInt should not be zero, because then theAnswer method would result in an error. So I make divideByThisInt private so that no one can make it zero.
In your program, I see no reason to make title private, because anyone can change it to anything. If I were you, I would either make it public and get rid of the get and set methods (making your program easier to read), or I would think about what kind of values I would not want title to have. For example, I think you probably don't want to have a title that is null or a string with no characters. So you should do this:

public void setTitle(String title) {
if (title != null){
if (!title.equals("")){
this.title = title;
}
}
}// end setTitle
 
Chris Rothgeb
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arulk: I am using regular text editor.

Roger: Thanks for the hint.

Kevin: Thank you for the advice and examples. I will redo based on your suggestion. One thing though. Because of these problems I have searched the internet for examples and such and found a program that mirrors my second code post. I corrected my code. Copied and ran their program which it did as one would expect. However, mine does not compile and run. I don't understand that. I have checked and double checked for errors. Case, etc. and I cannot find anything. Be that as it may, I will do what you are suggesting and see what happens. Thanks again all of you.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are still unable to solve your compilation problem, then post the code and error and we'll help.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic