0

I'm trying read matrix n by n which is stored in txt file. First line of the file specifying size of matrix :

My file look like :

5
1 2 3 4 5
2 3 4 6 8
5 6 8 7 4
4 9 9 9 9
4 4 7 8 2

Here is code snippet where what I've try is :

bufferedReader.readLine();
           for (int m = 0; m < size; m++) {
                String[] st = br.readLine().trim().split(" ");
                System.out.println(st);
                for (int n = 0;n < size; n++) {
                    inputArray[m][n] = Integer.parseInt(st[n]);
                    }
           }

but I get output like this after printing inputArray :

inputArray is :
1
1
1
1
1

please can anyone point what possibilities that I've missing. what is possible bug in this approach.

  • 1
    Besides the formatting your code looks okish, i.e. some things are missing: the definition of `size`, what value you set it to, how you print your output, what that `bufferedReader.readLine();` is meant to do, how `inputArray` is defined, how you read the file into `bufferedReader` etc. - you probably want to post a [mcve]. – Thomas Mar 02 '17 at 10:05
  • size of matrix is already read and the very first line bufferedReader.readLine(); skipping this first line – Kamalakar Thakare Mar 02 '17 at 10:07
  • Update: there's one strange thing: `Integer.parseInt(st[i])` - `i` probably is defined elsewhere and hence very likely wrong. I assume you want to use `n` here: `Integer.parseInt(st[n])`. Besides that you might want to check for the length of `st` since even if it should be equal to `size` I'd not count on that in any case (programming errors, data errors etc.). – Thomas Mar 02 '17 at 10:07
  • getting same output :-( – Kamalakar Thakare Mar 02 '17 at 10:15
  • Look at this: http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array <<< your mistake might be that you try to print out an array directly. Might be that your array actually contains the right data.Try System.out.println(Arrays.toString(st)); – Aziuth Mar 02 '17 at 10:15
  • I'll reiterate since you might have missed the edit of my first comment: we need a [mcve]. – Thomas Mar 02 '17 at 10:18

1 Answers1

1

Let your code do the talking. If you use clear method names and variable names, everyone should understand your code. Something like this (it uses Apache's FileUtils)

int[][] parseMatrixFile() {
    File file = getThatTxtFromSomewhere();
    List<String> lines = FileUtils.readLines(file, "utf-8");
    int matrixSize = Integer.parseInt(lines.get(0).trim());
    List<String> matrixList = lines.subList(1, matrixSize);
    int[][] matrix = getMatrix(matrixList, size);
    return matrix;
}

int[][] getMatrix(List<String> matrixLines, int size) {
    int[][] matrix = new int[size][];
    for (int row = 0; row < size; row++) {
        matrix[row] = getMatrixRow(matrixLines.get(row), size); 
    }
    return matrix;
}

int[] getMatrixRow(String row, int matrixSize) {
    int[] row = new int[matrixSize];
    String[] stringedRow = row.trim().split(" ");
    for (int i = 0; i < matrixSize; i++) 
        row[i] = Integer.parseInt(stringedRow[i]);
    return row;
}
stealthjong
  • 10,858
  • 13
  • 45
  • 84