Question by PatrickW: Having a little difficulty with this Java 6 programming…?
I bought this book “Sams Teach Yourself Java ™ 6 in 21 days” I am on day 6 and everything is going fairly well. However, this example I get to today (on pg 172 if you have the book) returns a number of errors. I am wondering if you you might have any suggestions of how to fix this problem.

Here is my “program:”


package org.cadenhead.ecommerce;

import java.util.*;

public class Storefront {
private LinkedList catalog = new LinkedList();

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);
}
}

So when I try to compile it, this is the error message that I get:
“Microsoft Windows [Version 6.0.6001]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.

C:\Users\Patrick>cd \

C:\>D:

D:\>cd dev/java/org/cadenhead/ecommerce

D:\dev\java\org\cadenhead\ecommerce>javac Storefront.java
Storefront.java:15: cannot find symbol
symbol : class Item
location: class org.cadenhead.ecommerce.Storefront
public Item getItem(int i) {
^
Storefront.java:10: cannot find symbol
symbol : class Item
location: class org.cadenhead.ecommerce.Storefront
Item it = new Item(id, name, price, quant);
^
Storefront.java:10: cannot find symbol
symbol : class Item
location: class org.cadenhead.ecommerce.Storefront
Item it = new Item(id, name, price, quant);
^
Storefront.java:16: cannot find symbol
symbol : class Item
location: class org.cadenhead.ecommerce.Storefront
return (Item)catalog.get(i);
^
Note: Storefront.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
4 errors”

Those carrots are under the “I” in “Item” for all those errors. I do not think that it is simply a capitalization error though, is it? I have no clue what that -Xlint thing is at the bottom of the error either.
Thank-you for your quick response. I apologize for my noobiness concerning Java. So I should probably explain a little further and see if you can help me more. I did define the Item class. It, however, is in a separate file. I believe that this is what the book is trying to teach me at this point: how to link two files together. Essentially, I am supposed to create the Storefront file this file will in turn access the Item file. I think what I need to do is to be able to say in the Storefront.java file “whenever I say ‘Item’ I am really referring to ‘Item.class’ ” I know that there is a way to do it, but it probably does not look like this: “Item = Item.class” Thank-you again for all your help.

Best answer:

Answer by Ben
Simply put, Java doesn’t know what an Item is. Did you define that class yourself? If not, you’re going to need to.

The warning about -Xlint refers to you using unsafe casting to Items. Java 5 introduced a way to write lists that looks like “LinkedList“, which says that the LinkedList can only hold items. In your case, someone could stick a String in your LinkedList and you would try to turn it into an Item anyway, which would crash the program. It’s just a warning, not an error. -Xlint is a program flag telling it to give you more details about warnings.

Know better? Leave your own answer in the comments!

2 thoughts on “Having a little difficulty with this Java 6 programming…?”

  1. Does the book expand on programs from previous chapters? If so, chances are you’re missing a class file from an earlier project that (among other things) defines class ‘Item’ and its methods. (If not, I’d make sure names are consistent – not specify ‘item’ in some places and ‘Item’ in others.)

  2. Ok, Ben is correct with his answer, but as you have added additional details I will answer them.

    Since you have this line at the top of your StoreFront class

    package org.cadenhead.ecommerce;

    It is in a package.

    What you need to check is if Item also has that line (exactly the same), meaning it is in the same Package.

    package org.cadenhead.ecommerce;

    If it is, hmm. I don’t know, apart from asking you to add that class as details, or put the code somewhere on the internet and provide a link to it.

    If it is not, as I suspect is it not, then you will need to have an import statement like you do here:

    import java.util.*;

    But with the package the Item class is in.
    Let’s assume that Item is in the
    org.cadenhead.other
    package. Then you would need:

    import org.cadenhead.other.*;
    or
    import org.cadenhead.other.Item;

    You can add extra time for the question to be answered, check out this page:
    What if my question doesn’t receive any answers?
    http://help.yahoo.com/l/us/yahoo/answers/ask/ask-55800.html;_ylt=Ag1gpxPONDRkuI7MNjw3q.B7jSN4

    Even though the topic seems a bit confusing, it is about extending the time period.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.