【问题标题】:Reading Text File in java while preserving white spaces using scanner class在使用扫描仪类保留空格的同时在 java 中读取文本文件
【发布时间】:2014-10-08 18:15:17
【问题描述】:

我想读取这个文本文件并将数据存储在一个二维数组中。每个空格都应替换为0,从而产生最终的 0 和 1 矩阵。正在发生的问题是Scanner 忽略了所有的空格,只将 1 放入矩阵中。

这是文本/输入文件。

                                       1111
                                      111111
                                    111 111   1
         111111                    11    111  11
       1111                         111  111    111
       1  1     11111 111           1 11  11        11111
     1111      111                        11111111
       1111     11111             111     1  11 11111
     111 111       1111           111            1111
       111       1111            11111      111111
       111      11 11                11111     111
          1111    11                11111
        1111                            11111
            1111 11 11                      1111
          111     111                        1 111
           11      1111                    111111
         1111      111                    1111
         1111     111                    1111
      1111 11      1111                   1111
        1111  111111                   11111
        111                              11111
          1111                        1111

Scanner fileScanner = new Scanner(new File("D:\Assignment.txt")); int [][] inputMatrix= new int[200][200];诠释 i=0,j=0; while (fileScanner.hasNextLine()) { String line = fileScanner.nextLine();扫描仪 lineScanner = new Scanner(line); while (lineScanner.hasNext()) { int token = Integer.parseInt(lineScanner.next());输入矩阵[i][j]=token; // 用记号 j++ 做任何需要做的事情; } lineScanner.close();我++; j=0; // 你在这行的末尾。做你该做的事。 } fileScanner.close();

【问题讨论】:

  • 分享您尝试过的所有内容,以便我们找出您代码中的问题。
  • 如何使用 readLine() 而不是每个字符读取字符,然后将该行中的所有空格替换为 0?
  • 扫描仪 fileScanner = new Scanner(new File("D:\\Assignment.txt")); int [][] inputMatrix= new int[200][200];诠释 i=0,j=0; while (fileScanner.hasNextLine()) { String line = fileScanner.nextLine();扫描仪 lineScanner = new Scanner(line); while (lineScanner.hasNext()) { int token = Integer.parseInt(lineScanner.next());输入矩阵[i][j]=token; // 用记号 j++ 做任何需要做的事情; } lineScanner.close();我++; j=0; // 你在这行的末尾。做你该做的事。 } fileScanner.close();
  • @user3678399 请不要添加代码作为注释,它完全不可读。编辑您的帖子,在此处添加和格式化。

标签: java java.util.scanner bufferedreader readfile fileinputstream


【解决方案1】:

读取行并将它们转换为二维数组:

List<String> lines = new ArrayList<>();
Scanner sc = new Scanner(new File("array.txt"));
while(sc.hasNextLine()) {
    lines.add(sc.nextLine());
}

// to array
int rows = lines.size();              // number of rows
int cols = 0;                         // number of columns
for(String line : lines) {
    cols = Math.max(cols, line.length());
}
int[][] array = new int[rows][cols];  // full of 0s
int i = 0;
for(String line : lines) {            // for each line, add the 1s
    char[] chars = line.toCharArray();
    for(int j = 0 ; j < chars.length ; ++j) {
        if(chars[j] == '1') {
            array[i][j] = 1;
        }
    }
    i++;
}

【讨论】:

    【解决方案2】:
    Scanner sc = new Scanner(file);
    String input = "";
    while (sc.hasNextLine()) {
        input += sc.nextLine()+"\n";
    }
    sc.close();
    String[] array = input.split("\n");
    

    现在您有一个行数组,您可以在每个字符串上使用replace(" ", "0"),然后使用toCharArray() 创建一个字符数组并将它们全部放入一个新数组中

    【讨论】:

    • 没有 hasNextLine() 返回布尔值吗?
    • 仍然是对 Scanner 的错误检查,因为它会抛出 NoSuchElementException - 如果没有找到任何行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    相关资源
    最近更新 更多