•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 425,999 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,717 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1750 | Replies: 6
![]() |
•
•
Join Date: Aug 2006
Location: Southern Illinois
Posts: 25
Reputation:
Rep Power: 3
Solved Threads: 0
I keep getting this error message whenever I try to compile a certain class or any classes that use this class:
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.
C:\J21work>javac GiftShop.java
C:\J21work\package\org\cadenhead\ecommerce\Storefront.java:25: cannot find symbo
l
symbol : method sort(java.util.LinkedList<java.lang.Object>)
location: class java.util.Collections
Collections.sort(catalog);
^
1 errorI 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
Student of Engineering,
Nerd Extraordinaire,
Not a Squirrel
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 18
Solved Threads: 199
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.
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.
•
•
Join Date: Aug 2006
Location: Southern Illinois
Posts: 25
Reputation:
Rep Power: 3
Solved Threads: 0
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:
The error comes when I try to compile the following code:
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.
package org.cadenhead.ecommerce;
import java.util.*;
public class Item implements Comparable {
private String id;
private String name;
private double retail;
private int quantity;
private double price;
Item(String idIn, String nameIn, String retailIn, String quanIn) {
id = idIn;
name = nameIn;
retail = Double.parseDouble(retailIn);
quantity = Integer.parseInt(quanIn);
if (quantity > 400)
price = retail * .5D;
else if (quantity > 200)
price = retail * .6D;
else
price = retail * .7D;
price = Math.floor( price * 100 + .5) / 100;
}
public int compareTo(Object obj) {
Item temp = (Item)obj;
if (this.price < temp.price)
return 1;
else if (this.price > temp.price)
return -1;
return 0;
}
public String getID() {
return id;
}
public String getName() {
return name;
}
public double getRetail() {
return retail;
}
public int getQuantity() {
return quantity;
}
public double getPrice() {
return price;
}
}The error comes when I try to compile the following code:
package org.cadenhead.ecommerce;
import java.util.*;
import org.cadenhead.ecommerce.*;
public class Storefront {
private LinkedList<Object> catalog = new LinkedList<Object>();
public void addItem(String id, String name, String price, String quant) {
Item it = new Item(id, name, price, quant);
catalog.add(it);
}
public Item getItem(int i) {
return (Item)catalog.get(i);
}
public int getSize() {
return catalog.size();
}
public void sort() {
Collections.sort(catalog);
}
}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
Student of Engineering,
Nerd Extraordinaire,
Not a Squirrel
•
•
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
I can't spot anything really wrong, but you could try importing this:
import java.util.Collections.*;
and see if that works.
import java.util.Collections.*;
and see if that works.
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 18
Solved Threads: 199
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.
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.
•
•
Join Date: Aug 2006
Location: Southern Illinois
Posts: 25
Reputation:
Rep Power: 3
Solved Threads: 0
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
Student of Engineering,
Nerd Extraordinaire,
Not a Squirrel
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- homework(easy): Java GUI/Netbeans using java.util.LinkedList (Java)
- JNDI/JDBC lookup problem with Sun Java Application Server 8.2 (Java)
- not to sound cliche, but...help me please! (Java)
Other Threads in the Java Forum
- Previous Thread: Best way to Design GUI
- Next Thread: Custom CharsetProvider doesn't get loaded in signed applet



Linear Mode