Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FileIO and mult. arrays

 
Roberto Diaz
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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," ") ;
while(token.hasMoreTokens())
{
int value = Integer.parseInt(token.nextToken());
studentarr[linesread][tokensread] = value ;
tokensread++;
}
linesread++;
}

// Some more code
}
}
***************************
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic