public static double[][] readMatrix(int numberOfRows,int numberOfColumns,boolean ignoreFirstLine,String fileName){
  double[][] matrix=new double[numberOfRows][numberOfColumns];
  try {
    FileReader fr=new FileReader(fileName);
    BufferedReader br=new BufferedReader(fr);
    String line;
    StringTokenizer tokenizer;
    String token;
    if (ignoreFirstLine) {
      br.readLine();
    }
    line=br.readLine();
    int rowId=0;
    while (line != null) {
      tokenizer=new StringTokenizer(line);
      for (int i=0; i < numberOfColumns; i++) {
        token=tokenizer.nextToken();
        double parsedNumber=Double.parseDouble(token);
        matrix[rowId][i]=parsedNumber;
      }
      if (tokenizer.hasMoreTokens()) {
        throw new RuntimeException("the number of columns is wrong");
      }
      line=br.readLine();
      rowId++;
    }
    if (rowId != numberOfRows) {
      throw new RuntimeException("the number of rows is wrong");
    }
  }
 catch (  RuntimeException e) {
    throw e;
  }
catch (  Exception e) {
    e.printStackTrace();
    throw new RuntimeException("Error reading the matrix from the file");
  }
  return matrix;
}

public static double [ ] [ ] readMatrix ( int numberOfRows , int numberOfColumns , boolean ignoreFirstLine , String fileName ) { double [ ] [ ] matrix = new double [ numberOfRows ] [ numberOfColumns ] ; try { FileReader fr = new FileReader ( fileName ) ; BufferedReader br = new BufferedReader ( fr ) ; String line ; StringTokenizer tokenizer ; String token ; if ( ignoreFirstLine ) { br . readLine ( ) ; } line = br . readLine ( ) ; int rowId = 0 ; while ( line != null ) { tokenizer = new StringTokenizer ( line ) ; for ( int i = 0 ; i < numberOfColumns ; i ++ ) { token = tokenizer . nextToken ( ) ; double parsedNumber = Double . parseDouble ( token ) ; matrix [ rowId ] [ i ] = parsedNumber ; } if ( tokenizer . hasMoreTokens ( ) ) { throw new RuntimeException ( "the number of columns is wrong" ) ; } line = br . readLine ( ) ; rowId ++ ; } if ( rowId != numberOfRows ) { throw new RuntimeException ( "the number of rows is wrong" ) ; } } catch ( RuntimeException e ) { throw e ; } catch ( Exception e ) { e . printStackTrace ( ) ; throw new RuntimeException ( "Error reading the matrix from the file" ) ; } return matrix ; }
reads in data from a file .
