aspose file tools*
The moose likes Beginning Java and the fly likes How to iterate through an Array of objects and a file at the same time? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to iterate through an Array of objects and a file at the same time? " Watch "How to iterate through an Array of objects and a file at the same time? " New topic
Author

How to iterate through an Array of objects and a file at the same time?

Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

I have a scenario where I have an Array of CurriculumSubject objects (which in turn has creted 15 JTextField object instances) and then a text file.

I want to iterate through the CurriculumSubject Array (and each JTextField for each subject) and go through the text file line by line, setting each JTextField to one line - and then move on to the next.

Currently, I have this method which receives a File object:



How can I integrate a BufferedReader in to this so that as I move through the 15 JTextFields, the BufferedReader reads the next line of the file?

TIA
Adam
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Why do you need a reader for text fields? Can’t you use their getText() method?
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

Campbell misunderstood. i see what you want to do. set the texFields from info in the file. if someone doesn't answer first i'll see if i can find something like that.
i havent read this but it seems like what you want
read file


SCJP
Visit my download page
Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

Thanks Randall - you are correct. The BufferedReader is to handle the text file and go through the file line by line. I already have managed to access the JTextFields through set and get, so that part of the problem is sorted - it's reading the file line by line that I really need help with. Thanks.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Adam Cripps wrote:it's reading the file line by line that I really need help with.

Well BufferedReader.readLine() returns the next line if there is one; or null if there isn't. Maybe that'll help.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

Thanks Winston. However, I will need to iterate through the file - how do I knit the two iterations together?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Adam Cripps wrote:Thanks Winston. However, I will need to iterate through the file - how do I knit the two iterations together?

I suggest you have a think about it and try something yourself. I've already given you all the information you need to write a read loop of your own using either for or while.

Winston
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

so true. often i post questions here and then after i think about it, i come up with the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to iterate through an Array of objects and a file at the same time?