View Single Post
  #2 (permalink)  
Old 10-02-2007, 02:19 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Some ideas. You could read the file, counting the number of lines, make your two-dimensional array with the number of lines. Then read it again and split each line into an array of Strings and parse each one to an int for the line array of ints.
Or you could start with a zero-length array, read the file once, parse each line to ints for the line array and add it to the array.
Or you could read the entire file as a string, count the number of lines and proceed as before.
You could use a BufferedReader or a Scanner for reading the file.
Code:
// Zero-length array int[][] arrayOfInts = new int[0][]; // Split a String String line = reader.readLine(); String[] tokens = line.split("\\s"); You could use a BufferedReader for the above or a Scanner. // Add a line array to the array int len = arrayOfInts.length; int[][] temp = new int[len][]; System.arraycopy(arrayOfInts, 0, temp, 0, len); temp[len] = newLineArray; arrayOfInts = temp; // For a BufferedReader BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(arg)))
Reply With Quote