简体   繁体   English

使用扫描仪类在保留空白的同时读取Java中的文本文件

[英]Reading Text File in java while preserving white spaces using scanner class

I want to read this text file and store the data in a 2D Array. 我想读取此文本文件并将数据存储在2D数组中。 Each white space should be replaced with a 0 , resulting in a final matrix of 0s and 1s. 每个空格都应替换为0 ,最终矩阵为0和1。 The problem that is occurring is that Scanner ignored all the white spaces and put only the 1s in the matrix. 出现的问题是Scanner忽略了所有空白并将仅1放入矩阵中。

This is text/input file. 这是文本/输入文件。

                                       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")); 扫描仪fileScanner =新扫描仪(新文件(“ D:\\ Assignment.txt”)); int [][] inputMatrix= new int[200][200]; int [] [] inputMatrix = new int [200] [200]; int i=0,j=0; int i = 0,j = 0; while (fileScanner.hasNextLine()) { String line = fileScanner.nextLine(); while(fileScanner.hasNextLine()){字符串行= fileScanner.nextLine(); Scanner lineScanner = new Scanner(line); 扫描仪lineScanner =新的扫描仪(行); while (lineScanner.hasNext()) { int token = Integer.parseInt(lineScanner.next()); while(lineScanner.hasNext()){int token = Integer.parseInt(lineScanner.next()); inputMatrix[i][j]=token; inputMatrix [i] [j] = token; // do whatever needs to be done with token j++; //用令牌j ++做任何需要做的事情; } lineScanner.close(); } lineScanner.close(); i++; i ++; j=0; j = 0; // you're at the end of the line here. //您在此行的结尾。 Do what you have to do. 做你该做的事。 } fileScanner.close(); } fileScanner.close();

Read the lines and convert them to a 2D array: 阅读这些行并将其转换为2D数组:

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++;
}
Scanner sc = new Scanner(file);
String input = "";
while (sc.hasNextLine()) {
    input += sc.nextLine()+"\n";
}
sc.close();
String[] array = input.split("\n");

now you have an array of lines, you can use replace(" ", "0") on each string and then toCharArray() to create an array of chars and put them all in a new array 现在您有了一个行数组,可以在每个字符串上使用replace(" ", "0") ,然后使用toCharArray()创建一个char数组并将它们全部放入一个新数组中

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

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