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

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 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.

Recommended Answers

All 6 Replies

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.

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:

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.

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

import java.util.Collections.*;

and see if that works.

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.

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.

no, it's the cast you're doing. You don't need that if you properly use generics.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.