This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problem with iterators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with iterators" Watch "Problem with iterators" New topic
Author

Problem with iterators

Ugluth Papadopoulos
Greenhorn

Joined: Apr 15, 2011
Posts: 7
Hello, I've been reading the Head First Design Patterns book and i've stumbled upon a problem. On the chapter about the iterators, i tried typing and compiling the code that the book gives, but i cant get it to compile.



Specifically I'm getting an error on: return new PancakeHouseMenuIterator(menuItems); this line in the end.
.\PancakeHouseMenu.java:26: cannot find symbol
symbol : constructor PancakeHouseMenuIterator(java.util.ArrayList<MenuItem>)
location: class PancakeHouseMenuIterator
return new PancakeHouseMenuIterator(menuItems);

Could somebody help me out with this one? I don't see what is wrong with it. By the way here's the PancakeHouseMenuIterator class, since i think it is pretty useful to help me resolving this problem.



If you need anything else please let me know, thanks in advance.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I think that the keyword void on line 8 in PancakeHouseMenuIterator is causing the problem. Try removing that.

Hutner


"If the facts don't fit the theory, get new facts" --Albert Einstein
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
I am surprised to see that hasNext() method. It is bad style to write if (...) return true; else return false; or similar. It would have been better written it like this
Ugluth Papadopoulos
Greenhorn

Joined: Apr 15, 2011
Posts: 7
I did try what you suggested, removing the void keyword from PancakeHouseIterator but then i get: PancakeHouseMenuIterator.java:8: invalid method declaration; return type required

Actually, I entered the void there, since I got this error, but i guess problems aren't that straightforward. About the other suggestion thank you, I didn't really put much thought in it, these examples are from the book, head first design patterns, so i just copied the code.

Any other suggestions about what might be wrong?
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

sorry didnt notice that the method signature was for PancakeHouseIterator not PancakeHouseMenuIterator.

change it to this:


If this is directly from the book, I'm surprised they missed this error.

Hunter
Ugluth Papadopoulos
Greenhorn

Joined: Apr 15, 2011
Posts: 7
Nop they didn't miss it, even though with the new version you do have to add the <MenuItem> on the ArrayList, since it was not needed for java 5 on which the book was published i think. But it was a typo error after all, instead of PancakeHouseMenuIterator I had PancakeHouseIterator...

God i hate when i look for a solution to such a problem for a long time Anyway, thank you very much for your input, greatly appreciated

Cheers!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with iterators
 
Similar Threads
JTable selected row problem
String combinations & permutations
Annotation @Stateful in EJB 3.0
Couldn't get Menu Composite Iterator (HeadFirst Design Pattern) to work
collections doubt