Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Reading from a file to a bidimensional array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading from a file to a bidimensional array" Watch "Reading from a file to a bidimensional array" New topic
Author

Reading from a file to a bidimensional array

Frank Ramirez
Greenhorn

Joined: Feb 12, 2010
Posts: 17
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
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading from a file to a bidimensional array
 
Similar Threads
Searching an Array
Bidimensional array to GUI?
java.lang.NullPointerException
Help with java input file
Help with java input file