943,793 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5497
  • Java RSS
Aug 12th, 2006
0

problem with java.util.Collections

Expand Post »
I keep getting this error message whenever I try to compile a certain class or any classes that use this class:

Java Syntax (Toggle Plain Text)
  1. C:\J21work>javac GiftShop.java
  2. C:\J21work\package\org\cadenhead\ecommerce\Storefront.java:25: cannot find symbo
  3. l
  4. symbol : method sort(java.util.LinkedList<java.lang.Object>)
  5. location: class java.util.Collections
  6. Collections.sort(catalog);
  7. ^
  8. 1 error

I held off as long as I could from posting a query about it here, I've been searching online through documentation for java to see if I could learn about why this sort method isn't working...alas, I just don't know enough about this language yet to know exactly what's going on. I can only look through the code and rewrite it so many times trying to eliminate spelling errors. Frankly, by the fiftieth time, I get more than a little frustrated with it so if someone could please give me a clue as to why this method will not work, I'd be much obliged. Thanks in advance for any help you can offer.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
mrsteve is offline Offline
25 posts
since Aug 2006
Aug 13th, 2006
0

Re: problem with java.util.Collections

What is the EXACT type of your list?

According to the API specs the elements of the List should all be the same type and all must implement Comparable.
If that's not the case you can indeed get that error, as you're passing it an argument that doesn't conform to the specification.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 13th, 2006
0

Re: problem with java.util.Collections

Ok, this first bit of code compiles with no problem, I'm just showing it so you can see that the objects all implement comparable:

Java Syntax (Toggle Plain Text)
  1. package org.cadenhead.ecommerce;
  2.  
  3. import java.util.*;
  4.  
  5. public class Item implements Comparable {
  6. private String id;
  7. private String name;
  8. private double retail;
  9. private int quantity;
  10. private double price;
  11.  
  12. Item(String idIn, String nameIn, String retailIn, String quanIn) {
  13. id = idIn;
  14. name = nameIn;
  15. retail = Double.parseDouble(retailIn);
  16. quantity = Integer.parseInt(quanIn);
  17.  
  18. if (quantity > 400)
  19. price = retail * .5D;
  20. else if (quantity > 200)
  21. price = retail * .6D;
  22. else
  23. price = retail * .7D;
  24. price = Math.floor( price * 100 + .5) / 100;
  25. }
  26.  
  27. public int compareTo(Object obj) {
  28. Item temp = (Item)obj;
  29. if (this.price < temp.price)
  30. return 1;
  31. else if (this.price > temp.price)
  32. return -1;
  33. return 0;
  34. }
  35.  
  36. public String getID() {
  37. return id;
  38. }
  39.  
  40. public String getName() {
  41. return name;
  42. }
  43.  
  44. public double getRetail() {
  45. return retail;
  46. }
  47.  
  48. public int getQuantity() {
  49. return quantity;
  50. }
  51.  
  52. public double getPrice() {
  53. return price;
  54. }
  55. }


The error comes when I try to compile the following code:

Java Syntax (Toggle Plain Text)
  1. package org.cadenhead.ecommerce;
  2.  
  3. import java.util.*;
  4. import org.cadenhead.ecommerce.*;
  5.  
  6.  
  7. public class Storefront {
  8. private LinkedList<Object> catalog = new LinkedList<Object>();
  9.  
  10. public void addItem(String id, String name, String price, String quant) {
  11.  
  12. Item it = new Item(id, name, price, quant);
  13. catalog.add(it);
  14. }
  15.  
  16. public Item getItem(int i) {
  17. return (Item)catalog.get(i);
  18. }
  19.  
  20. public int getSize() {
  21. return catalog.size();
  22. }
  23.  
  24. public void sort() {
  25. Collections.sort(catalog);
  26. }
  27. }

Before, I was having problems due to a lack of using generics (which I'm still a little confused about, to be honest). The problem with all of this is that everything should work as the author wrote it...(this is code from a Java book). Anyway, I think the exact type of the list would be Object? I'm not sure...anyway, since all the elements of the list are Item objects they are all of the same type.
Reputation Points: 11
Solved Threads: 0
Light Poster
mrsteve is offline Offline
25 posts
since Aug 2006
Aug 13th, 2006
0

Re: problem with java.util.Collections

I can't spot anything really wrong, but you could try importing this:

import java.util.Collections.*;

and see if that works.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Aug 13th, 2006
0

Re: problem with java.util.Collections

Collections is a class inside java.util, so it's imported.

The problem is that he declared his List as List<Object> and Object doesn't implement Comparable.
As a result Collections.sort(Collection<T implements Comparable>) isn't going to work because it's being passed a Collection which is not of type Collection<T implements Comparable>.
Had he declared his List as he should have, List<Item> = new LinkedList<Item> it would have compiled correctly and he'd have the typesafety of generics at the same time instead of just a dirty hack to prevent compiler warnings.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 13th, 2006
0

Re: problem with java.util.Collections

actually, that makes sense...putting in the Object's name rather than just putting in 'Object'. But now the problem that I fixed by using generics in the first place is happening again...I get an unchecked or unsafe operations warning. I think it has something to do with the sort method in the Collections class, although I don't think I'm misusing it... Hmm. Someday, I will understand what I'm doing. Today, however, seems like it will not be that day.
Reputation Points: 11
Solved Threads: 0
Light Poster
mrsteve is offline Offline
25 posts
since Aug 2006
Aug 14th, 2006
0

Re: problem with java.util.Collections

no, it's the cast you're doing. You don't need that if you properly use generics.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Best way to Design GUI
Next Thread in Java Forum Timeline: Custom CharsetProvider doesn't get loaded in signed applet





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC