繁体   English   中英

如何将txt文件读入2d Array Java

[英]How to read a txt file into a 2d Array Java

快速问题,我如何将txt文件读入二维数组? 我需要将每行分成一个数组,以便以后用它来测量长度和宽度的总和。 这将用于生命游戏。

public P4_Icel_Murad_Life(String fileName) {
        Scanner in;
        try {
            Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
            int matrix[][] = new int[size][size];
            while (s.hasNext()) {
                for (int i = 0; i < matrix.length; i++) {
                       for (int col = 0; col < matrix.length; col++) {
                      //      matrix[i][col] = ;
                        }
                }
            }
            s.close();

        } catch (IOException i) {
            System.out.println("Problems..");

        }
    }

文字文件

20 20
    0    2
    0    6
    0    7
    0   10
    0   11
    0   16
    1    0
    1    4
    1    7
    2    6
    2   12
    2   15
    2   19
    3    0
    3    4
    3   14
    3   16
    3   17
    4    0
    4    8
    4   12
    4   14
    4   15
    5    0
    5    2
    5    4
    5    7
    5   13
    5   17
    6   11
    6   14
    6   15
    6   18
    7    4
    7   10
    7   13
    7   14
    8    6
    8   11
    8   12
    8   14
    8   17
    8   19
    9    6
    9    8
    9   13
    9   14
   10    3
   10   10
   10   19
   11    6
   11    7
   11    9
   11   10
   11   17
   12    0
   12    2
   12   12
   13    0
   13    1
   13    7
   13    8
   13   14
   13   15
   14    7
   14    9
   14   12
   14   13
   14   16
   14   18
   15    0
   15    2
   15    6
   15    8
   15   10
   15   11
   15   16
   15   17
   16    2
   16    3
   16   18
   17    2
   17    7
   17   11
   17   13
   17   19
   18    0
   18    5
   18    6
   18   11
   18   12
   18   13
   18   14
   19    3
   19    4
   19    6
   19    8
   19   11
   19   16
   19   17

因此,文本文件似乎包含生命游戏中生命所在的所有对。

你所做的是增加每次读数的“i”值,但这并不保证。

相反,以下协议是它看起来的文本文件之一。

  1. 线包含板的X和Y尺寸。

其余的线条包含有生命的地方。

因此,您的代码看起来应该更像这样(除了您已提供的内容之外,不包括异常处理)

 public P4_Icel_Murad_Life(String fileName) {
        Scanner in;
        try {
            Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
            int rows = s.nextInt();
            int columns = s.nextInt();
            int matrix[][] = new int[rows][columns];
            while (s.hasNextInt()) {
                int row = s.nextInt();
                int col = s.nextInt(); // If this fails the text documnet is incorrect.
                matrix[row][col] = 1;
            }
            s.close();
            // At this point all dead cells are 0 and all live are 1
        } catch (IOException i) {
            System.out.println("Problems..");

        }
    }
public int[][] P4_Icel_Murad_Life(String fileName) {
    try {
        BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));

        // n = Rownum ; m = Colnum
        int array[][] = new int[n][m];
        String line = reader.readLine();

        int i = 0, j = 0;
        while (line != null) {
            String strArray[] = line.split(" ");

            if (!line.trim().isEmpty()) {
                for (String s : strArray) {
                    if (!s.trim().isEmpty()) {
                        array[i][j++] = Integer.parseInt(s);
                    }
                }
                line = reader.readLine();
                i++;
                j = 0;
            }
        }
        reader.close();
        return array;
    } catch (IOException ex) {
        System.out.println("Problems..");
    }
    return null;
}

您对int值感兴趣,所以请使用它。

此外, hasNextLine条件已移入循环,并且hasNextInt条件已添加到columns循环中。

public P4_Icel_Murad_Life(String fileName) {
        Scanner in;
        try {
            Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
            int matrix[][] = new int[size][size];

                for (int i = 0; i < matrix.length && s.hasNextLine(); i++) {
                       for (int col = 0; col < matrix.length && s.hasNextInt(); col++) {
                           matrix[i][col] = s.nextInt() ;

                        }
                       s.nextLine(); // col values populated for this row, time to go to the next line
                }

            s.close();

        } catch (IOException i) {
            System.out.println("Problems..");

        }
    }

您可以使用opencsv来读写这种文件

 CSVReader reader = new CSVReader(
                new FileReader("C:/Users/muro0/Desktop/life100.txt"), ' ');
 // Read all rows at once
 List<String[]> allRows = readAll(reader);

它将文件读取到字符串,但您可以将其转换为整数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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