problem with java.util.Collections

Reply

Join Date: Aug 2006
Posts: 25
Reputation: mrsteve is an unknown quantity at this point 
Solved Threads: 0
mrsteve's Avatar
mrsteve mrsteve is offline Offline
Light Poster

problem with java.util.Collections

 
0
  #1
Aug 12th, 2006
I keep getting this error message whenever I try to compile a certain class or any classes that use this class:

  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.
Steve Warren,
Student of Engineering,
Nerd Extraordinaire,
Not a Squirrel
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: problem with java.util.Collections

 
0
  #2
Aug 13th, 2006
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 25
Reputation: mrsteve is an unknown quantity at this point 
Solved Threads: 0
mrsteve's Avatar
mrsteve mrsteve is offline Offline
Light Poster

Re: problem with java.util.Collections

 
0
  #3
Aug 13th, 2006
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:

  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:

  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.
Steve Warren,
Student of Engineering,
Nerd Extraordinaire,
Not a Squirrel
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: problem with java.util.Collections

 
0
  #4
Aug 13th, 2006
I can't spot anything really wrong, but you could try importing this:

import java.util.Collections.*;

and see if that works.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: problem with java.util.Collections

 
0
  #5
Aug 13th, 2006
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 25
Reputation: mrsteve is an unknown quantity at this point 
Solved Threads: 0
mrsteve's Avatar
mrsteve mrsteve is offline Offline
Light Poster

Re: problem with java.util.Collections

 
0
  #6
Aug 13th, 2006
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.
Steve Warren,
Student of Engineering,
Nerd Extraordinaire,
Not a Squirrel
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: problem with java.util.Collections

 
0
  #7
Aug 14th, 2006
no, it's the cast you're doing. You don't need that if you properly use generics.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC