Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem on Generic example from K&B study guide

 
Chidimma Juliana
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5333
48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We won't know, unless you post the error, and the Adder class.
 
Chidimma Juliana
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iterator it = List.iterator();

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

 
Chidimma Juliana
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have corrected List to list which was used to invoke the iterator method. But the compilation still fails.



 
John Schubert
Ranch Hand
Posts: 39
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles fine on my machine.
 
Pranav Raulkar
Ranch Hand
Posts: 73
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic