public CostMatrix(Reader reader) throws Exception {
  LineNumberReader lnr=new LineNumberReader(reader);
  String line;
  int currentRow=-1;
  while ((line=lnr.readLine()) != null) {
    if (line.startsWith("%")) {
      continue;
    }
    StringTokenizer st=new StringTokenizer(line);
    if (!st.hasMoreTokens()) {
      continue;
    }
    if (currentRow < 0) {
      int rows=Integer.parseInt(st.nextToken());
      if (!st.hasMoreTokens()) {
        throw new Exception("Line " + lnr.getLineNumber() + ": expected number of columns");
      }
      int cols=Integer.parseInt(st.nextToken());
      if (rows != cols) {
        throw new Exception("Trying to create a non-square cost " + "matrix");
      }
      m_size=rows;
      initialize();
      currentRow++;
      continue;
    }
 else {
      if (currentRow == m_size) {
        throw new Exception("Line " + lnr.getLineNumber() + ": too many rows provided");
      }
      for (int i=0; i < m_size; i++) {
        if (!st.hasMoreTokens()) {
          throw new Exception("Line " + lnr.getLineNumber() + ": too few matrix elements provided");
        }
        String nextTok=st.nextToken();
        Double val=null;
        try {
          val=new Double(nextTok);
        }
 catch (        Exception ex) {
          val=null;
        }
        if (val == null) {
          setCell(currentRow,i,nextTok);
        }
 else {
          setCell(currentRow,i,val);
        }
      }
      currentRow++;
    }
  }
  if (currentRow == -1) {
    throw new Exception("Line " + lnr.getLineNumber() + ": expected number of rows");
  }
 else   if (currentRow != m_size) {
    throw new Exception("Line " + lnr.getLineNumber() + ": too few rows provided");
  }
}

public CostMatrix ( Reader reader ) throws Exception { LineNumberReader lnr = new LineNumberReader ( reader ) ; String line ; int currentRow = - 1 ; while ( ( line = lnr . readLine ( ) ) != null ) { if ( line . startsWith ( "%" ) ) { continue ; } StringTokenizer st = new StringTokenizer ( line ) ; if ( ! st . hasMoreTokens ( ) ) { continue ; } if ( currentRow < 0 ) { int rows = Integer . parseInt ( st . nextToken ( ) ) ; if ( ! st . hasMoreTokens ( ) ) { throw new Exception ( "Line " + lnr . getLineNumber ( ) + ": expected number of columns" ) ; } int cols = Integer . parseInt ( st . nextToken ( ) ) ; if ( rows != cols ) { throw new Exception ( "Trying to create a non-square cost " + "matrix" ) ; } m_size = rows ; initialize ( ) ; currentRow ++ ; continue ; } else { if ( currentRow == m_size ) { throw new Exception ( "Line " + lnr . getLineNumber ( ) + ": too many rows provided" ) ; } for ( int i = 0 ; i < m_size ; i ++ ) { if ( ! st . hasMoreTokens ( ) ) { throw new Exception ( "Line " + lnr . getLineNumber ( ) + ": too few matrix elements provided" ) ; } String nextTok = st . nextToken ( ) ; Double val = null ; try { val = new Double ( nextTok ) ; } catch ( Exception ex ) { val = null ; } if ( val == null ) { setCell ( currentRow , i , nextTok ) ; } else { setCell ( currentRow , i , val ) ; } } currentRow ++ ; } } if ( currentRow == - 1 ) { throw new Exception ( "Line " + lnr . getLineNumber ( ) + ": expected number of rows" ) ; } else if ( currentRow != m_size ) { throw new Exception ( "Line " + lnr . getLineNumber ( ) + ": too few rows provided" ) ; } }
reads a matrix from a reader . the first line in the file should contain the number of rows and columns . subsequent lines contain elements of the matrix . ( fracpete : taken from old weka . core . matrix class )
