to print the text file in 2d matrix of same dimension given in file using java -


possible duplicate:
how print 2d array .txt file in java

text file is:

8.00 28.00   18.00 28.00  8.00 23.00   12.00 20.00  15.00 30.00  ... etc (many more lines) 

i reached upto:

public class asgn2backup {     public static double[][] matrix;      public static void main(final string[] args) throws ioexception {         system.out.print("enter name of file: ");         final string filename = readinput();          final bufferedreader br = new bufferedreader(              new filereader(filename + ".txt"));         string line;         int order = 0;          int rowindex = 0;         int counter = 0;         while ((line = br.readline()) != null) {             counter++;             if (counter == 1) {                 order = integer.parseint(line);                 matrix = new double[order][order];                 system.out.println("order: " + order);             }              if (counter == 2) {                  final string source = line;                 system.out.println("source: " + source);             }              if (counter != 2 && counter != 1) {                 order = integer.parseint(line);                 matrix = new double[order][order];                 system.out.println("order: " + order);                 final stringtokenizer theline =                      new stringtokenizer(line, ", ");                 int colindex = 0;                 while (theline.hasmoretokens()) {                     final string st = theline.nexttoken();// .trim();                     matrix[rowindex][colindex] = double.parsedouble(st);                     colindex = colindex + 1;                 }                 rowindex = rowindex + 1;             }          }          (int x = 0; x < matrix.length - 1; x++) {             (int p = 0; p < matrix.length - 1; p++) {                 system.out.print(matrix[x][p] + " ");             }         }          br.close();     }      private static string readinput() {         try {             final bufferedreader in = new bufferedreader(                 new inputstreamreader(system.in));             return in.readline();         } catch (final ioexception e) {         }         return "";     }  } 

but gives numberformatexception runtime error. give me complete solution. pls me.

the parser not fit input file @ all. in each condition try parse entire line single integer value. cause numberformatexceptions.

example:

if (counter != 2 && counter != 1) {     order = integer.parseint(line);  // line = "8.00 23.00" < not integer 

the lines contain float or double values. you'll have split line around multiple whitespaces , parse 2 fragments double.parsedouble(split[<0|1>]) double values.


Comments