JavaRanch » Java Forums »
Java »
Beginning Java
| Author |
Sorting Arrays
|
Chris Rothgeb
Greenhorn
Joined: Mar 15, 2008
Posts: 11
|
|
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
Joined: Mar 22, 2008
Posts: 265
|
|
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 ]
|
There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
|
 |
Chris Rothgeb
Greenhorn
Joined: Mar 15, 2008
Posts: 11
|
|
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
Joined: Jan 10, 2002
Posts: 56228
|
|
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.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Chris Rothgeb
Greenhorn
Joined: Mar 15, 2008
Posts: 11
|
|
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
Joined: Sep 29, 2002
Posts: 1683
|
|
|
Are you sure that the gettitle method exists? Hint: Java is case sensitive.
|
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
|
 |
arulk pillai
Author
Ranch Hand
Joined: May 31, 2007
Posts: 3190
|
|
|
Are you using any IDE like eclipse or just using a text pad?
|
Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
|
 |
Kevin Tysen
Ranch Hand
Joined: Oct 12, 2005
Posts: 255
|
|
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
Joined: Mar 15, 2008
Posts: 11
|
|
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
Joined: Sep 29, 2002
Posts: 1683
|
|
|
If you are still unable to solve your compilation problem, then post the code and error and we'll help.
|
 |
 |
|
|
subject: Sorting Arrays
|
|
|
|