• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Reading from a file to a bidimensional array

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody
I'm working on a project for school and part of it is to read from a txt file and convert to a bidimensional array.
Here is the code we created for that:



As you can see, we created a method to determine the size of the array (specially the number of rows) and another method to populate the array.
What bothers me is that we have to read the file twice, once for each method.
Is there a way that I can combine both so I read the file only once?

Thank you in advance
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the file into a List<List<String>> or a List<String[]>, if you know in advance how many columns are contained in each row -- you'll probably want to use the ArrayList implementation of List -- then after populating the list, if you really need a bidimensional array you can generate it using List#toArray. But you probably shouldn't need that, it would be better to refactor your code to use the List<List> or List<String[]> instead.
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic