Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with iterators

 
Ugluth Papadopoulos
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the keyword void on line 8 in PancakeHouseMenuIterator is causing the problem. Try removing that.

Hutner
 
Campbell Ritchie
Sheriff
Posts: 48452
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic