aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem on Generic example from K&B study guide Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem on Generic example from K&B study guide" Watch "Problem on Generic example from K&B study guide" New topic
Author

Problem on Generic example from K&B study guide

Chidimma Juliana
Greenhorn

Joined: Jul 14, 2011
Posts: 18
Please could someone help ,

I tried to compile the program below but it fails;



What could be the problem?

Thank you
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

We won't know, unless you post the error, and the Adder class.
Chidimma Juliana
Greenhorn

Joined: Jul 14, 2011
Posts: 18
Hello Stephan,

Am sorry providing incomplete information.



Below is compiler error message.

C:\Documents and Settings\Chidimma\Desktop\Allfiles>javac TestLegacy.java
TestLegacy.java:5: non-static method iterator() cannot be referenced from a stat
ic context
Iterator it = List.iterator();
^
TestLegacy.java:8: cannot find symbol
symbol : method intValue()
location: class Integer
int i = ((Integer)it.next()).intValue();
^
TestLegacy.java:19: cannot find symbol
symbol : method add(int)
location: interface java.util.List<Integer>
myList.add(4);
^
TestLegacy.java:20: cannot find symbol
symbol : method add(int)
location: interface java.util.List<Integer>
myList.add(6);
^
.\Integer.java:9: cannot find symbol
symbol : constructor Integer()
location: class Integer
Integer y = new Integer();
^
.\Integer.java:11: cannot find symbol
symbol : method intValue()
location: class Integer
int x = y.intValue();




Thank you
ayatullah syauqi qasthalani
Greenhorn

Joined: Apr 01, 2011
Posts: 3
Iterator it = List.iterator();

it should be :
Iterator it = list.iterator();

Chidimma Juliana
Greenhorn

Joined: Jul 14, 2011
Posts: 18
I have corrected List to list which was used to invoke the iterator method. But the compilation still fails.



John Schubert
Ranch Hand

Joined: Sep 21, 2011
Posts: 39

It compiles fine on my machine.
Pranav Raulkar
Ranch Hand

Joined: Apr 20, 2011
Posts: 73

Compiles and runs fine on my machine too.

I guess you are not using any IDE to run this. I copied the code into Eclipse and changed compiler compliance level. I can see that 1.6 and 1.5 work just fine, but setting it to 1.4 shows problem in Eclipse for Integer.
Chidimma Juliana
Greenhorn

Joined: Jul 14, 2011
Posts: 18
Hello,

Am using command prompt and compiler is jdk, below is the version of the compiler;

C:\Documents and Settings\Chidimma\Desktop\Allfiles>javac -version
javac 1.6.0_26
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Chidimma Juliana wrote:
C:\Documents and Settings\Chidimma\Desktop\Allfiles>javac -version
javac 1.6.0_26

It works even on command prompt.Your version is also valid.


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
 
wood burning stoves
 
subject: Problem on Generic example from K&B study guide