简体   繁体   中英

Java Reading a 2D array from a file

Say I have a file of the following format:

3
4
1,2,3,4
5,6,7,8
9,10,11,12

The first two lines of the file represent the number of rows and columns of a 2D array. Thereafter, each line represents the values of each row of the 2D array. I am trying to read this file and create a 2D integer array in java. I tried the following code:

public class PrintSpiral {
    private static BufferedReader in = null;
    private static int rows = 0;
    private static int columns = 0;
    private static int [][] matrix = null;
    public static void main(String []args) throws Exception {
        try {
            String filepath = args[0];
            int lineNum = 0;

            int row=0;
            in = new BufferedReader(new FileReader(filepath));
            String line = in.readLine();
            while(line!=null) {
                lineNum++;
                if(lineNum==1) {
                    rows = Integer.parseInt(line);
                    System.out.println("The number of rows of the matrix are: " + rows);
                } else if(lineNum==2) {
                    columns = Integer.parseInt(line);
                    System.out.println("The number of columns of the matrix are: " + columns);
                    matrix = new int[rows][columns];
                } else {
                    String [] tokens = line.split(",");
                    for (int j=0; j<tokens.length; j++) {
                        System.out.println("I am filling the row: " + row);
                        matrix[row][j] = Integer.parseInt(tokens[j]);
                    }
                    row++;
                }
            }
        } catch (Exception ex) {
            System.out.println("The code throws an exception");
            System.out.println(ex.getMessage());
        } finally {
            if (in!=null) in.close();
        }
        System.out.println("I am printing the matrix: ");
        for (int i=0; i < rows; i++) {
            for(int j=0; j < columns; j++)
                System.out.print(matrix[i][j]);
            System.out.println("");
        }
    }
}

The output that I see is:

The number of rows of the matrix are: 3
The number of columns of the matrix are: 3
I am filling the row: 0
I am filling the row: 1
I am filling the row: 2
I am filling the row: 3
The code throws an exception
3
I am printing the matrix: 
300
300
300
3 0 0 0 0 0 3 3 0

Clearly, the java code is not reading the file correctly. Also, my code throws an exception. I am not sure what causes this exception. I can't seem to figure out what's wrong with my code. Thanks!

change

in = new BufferedReader(new FileReader(filepath));
String line = in.readLine();
while(line!=null) { .....

to

in = new BufferedReader(new FileReader(filepath));
String line = null;
while((line = in.readLine()) !=null) { .....

to read a new line at the start of each loop

You (very obviously) don't have a readLine() inside your loop, so it only ever reads the first line of the file.

Why bother having to specify the rows/columns? Why not just figure it out from the data itself?

at the end of the while loop, add in.readline();

you are just keeping the same line forever and looping on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM