Java从文件中读取二维数组

发布于 2024-12-10 06:30:38 字数 2340 浏览 0 评论 0原文

假设我有一个以下格式的文件:

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

文件的前两行代表二维数组的行数和列数。此后,每一行代表二维数组的每一行的值。我正在尝试读取此文件并在 java 中创建一个 2D 整数数组。我尝试了以下代码:

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 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

显然,java 代码没有正确读取文件。另外,我的代码抛出异常。我不确定是什么导致了这个异常。我似乎无法弄清楚我的代码出了什么问题。谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧情勿念 2024-12-17 06:30:38

更改

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

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

在每个循环开始时读取新行

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

好多鱼好多余 2024-12-17 06:30:38

您(非常明显)循环内没有 readLine() ,因此它只读取文件的第一行。

为什么要费心指定行/列?为什么不直接从数据本身中找出答案呢?

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?

一身仙ぐ女味 2024-12-17 06:30:38

在 while 循环的末尾,添加 in.readline();

您只是永远保留同一行并在其上循环。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文