Question:
Having a little difficulty with this Java 6 programming...?
PatrickW
2009-09-07 17:08:12 UTC
I bought this book "Sams Teach Yourself Java (tm) 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.
Three answers:
Mark aka jack573
2009-09-10 19:51:24 UTC
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.
Ben
2009-09-07 17:16:14 UTC
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.
TheMadProfessor
2009-09-08 10:19:39 UTC
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.)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...