Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 41
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 74757
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you need a reader for text fields? Can’t you use their getText() method?
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Winston. However, I will need to iterate through the file - how do I knit the two iterations together?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so true. often i post questions here and then after i think about it, i come up with the answer.
 
reply
    Bookmark Topic Watch Topic
  • New Topic