File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes FileIO and mult. arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "FileIO and mult. arrays" Watch "FileIO and mult. arrays" New topic

FileIO and mult. arrays

Roberto Diaz
Ranch Hand

Joined: Mar 08, 2002
Posts: 40
I am reading a simple text file and trying to
copy the input into a multidimensional array.
I can copy the contents into the mult. array,
if I declare the dimensions of the array while
initializing. I want to do it on the fly based
on the number of lines read from file and number of tokens in each line.
Is there a way out ??
Code is as follows
public class File_I_O{
// Some code
private int[][] studentarr = new int[20][20] ; ---> want to do this dynamically !!
public void ReadFileIO(String filename) throws IOException
String inputline ;
int linesread = 0,tokensread = 0 ;
FileReader inputfile = new FileReader(filename);
BufferedReader inp = new BufferedReader(inputfile);

while( (inputline = inp.readLine()) != null)
tokensread = 0 ;
StringTokenizer token = new StringTokenizer(inputline," ") ;
int value = Integer.parseInt(token.nextToken());
studentarr[linesread][tokensread] = value ;

// Some more code
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Were you thinking along these lines:

Of course, you'd be counting the number of lines in the file and the number of tokens to set your array lengths.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: FileIO and mult. arrays
It's not a secret anymore!