• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array list and Iterator

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Jin.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you are running this to test it, do you have

uncommented?

That would explain things.
 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To clarify iter.next() is returning the next value and moving the iterator on.
So if you System.out(iter.next());
and then return(iter.next());
then you will take your 10 values and output 5 of them to the screen and return the other 5 from the function.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Please UseCodeTags when you post source code - I've added them for you in your post above.
 
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it....one 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
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your kind advice.

The solution posted by Dhan has resolved the issue.

Cheers

Jin.
 
reply
    Bookmark Topic Watch Topic
  • New Topic