File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Array list and Iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array list and Iterator" Watch "Array list and Iterator" New topic

Array list and Iterator

jin kazama

Joined: Mar 09, 2010
Posts: 7
Good evening, can I ask for some guidance with an issue I have with a small program?

I am having problems with a couple of classes using an ArrayList and an Iterator with a scanner reading in a file. When the file has been read in and another class attempts to return the elements in the Iterator, it only reports back every other entry from the file. So the file File1.txt contains 10 rows of Computer names, but when the program runs it only reports back 5 of the names.

The Class data is:

The next class is:

Thank you in advance.

Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
When you are running this to test it, do you have


That would explain things.

Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
To clarify is returning the next value and moving the iterator on.
So if you System.out(;
and then return(;
then you will take your 10 values and output 5 of them to the screen and return the other 5 from the function.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15040

Welcome to JavaRanch.

Please UseCodeTags when you post source code - I've added them for you in your post above.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Dhan Kumar

Joined: Aug 03, 2009
Posts: 29

I got different reason which exactly explains the flaw.
See the below code

getTheData method is called twice that explains why you get only half of the text from file.

try this : this will work.
jin kazama

Joined: Mar 09, 2010
Posts: 7
Thank you all for your kind advice.

The solution posted by Dhan has resolved the issue.


It is sorta covered in the JavaRanch Style Guide.
subject: Array list and Iterator
jQuery in Action, 3rd edition