public static SparseMatrix loadSparseMatrix(File file) throws IOException, IllegalFormatException {
  String str;
  TextFileReader reader=new TextFileReader(file);
  str=reader.readLine();
  String[] parts=str.split("\\s+");
  if (parts.length != 2)   throw new IllegalFormatException();
  int rows=Integer.parseInt(parts[0]);
  int cols=Integer.parseInt(parts[1]);
  SparseMatrix matrix=new SparseMatrix(rows,cols);
  while ((str=reader.readLine()) != null) {
    parts=str.split("\\s+");
    if (parts.length != 3)     throw new IllegalFormatException();
    int row=Integer.parseInt(parts[0]);
    int col=Integer.parseInt(parts[1]);
    double value=Double.parseDouble(parts[2]);
    matrix.add(row,col,value);
  }
  reader.close();
  return matrix;
}

public static SparseMatrix loadSparseMatrix ( File file ) throws IOException , IllegalFormatException { String str ; TextFileReader reader = new TextFileReader ( file ) ; str = reader . readLine ( ) ; String [ ] parts = str . split ( "\\s+" ) ; if ( parts . length != 2 ) throw new IllegalFormatException ( ) ; int rows = Integer . parseInt ( parts [ 0 ] ) ; int cols = Integer . parseInt ( parts [ 1 ] ) ; SparseMatrix matrix = new SparseMatrix ( rows , cols ) ; while ( ( str = reader . readLine ( ) ) != null ) { parts = str . split ( "\\s+" ) ; if ( parts . length != 3 ) throw new IllegalFormatException ( ) ; int row = Integer . parseInt ( parts [ 0 ] ) ; int col = Integer . parseInt ( parts [ 1 ] ) ; double value = Double . parseDouble ( parts [ 2 ] ) ; matrix . add ( row , col , value ) ; } reader . close ( ) ; return matrix ; }
the first line of the input file should be " rows cols " , and the following line should be " rowindex colindex value "
