• 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

FileIO and mult. arrays

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
}
***************************
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic