简体   繁体   English

Java从文件读取字母到多维数组

[英]Java Read alphabet from file to multi-dimensional array

This is definitely a logic issue first and foremost but I can't seem to work out how to solve this: 首先,这绝对是一个逻辑问题,但我似乎无法解决该问题:

I have a .txt file that I am reading in with the entire alphabet made of 1's and 0's, for an example this is a B followed by a C in the .txt file: 我有一个.txt文件,正在读取,整个字母由1和0组成,例如,在.txt文件中,这是一个B,然后是C:

0 0 1 1 1 1 0 0 0
0 0 1 0 0 0 1 0 0
0 0 1 1 1 1 0 0 0
0 0 1 0 0 0 1 0 0
0 0 1 1 1 1 0 0 0

0 0 0 1 1 1 0 0 0
0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0

You can make out the letters by following the 1's. 您可以按照1来区分字母。 What I need to do is read in each of these letters into an array alphabet, each letter is split by an empty line and the letters have to follow this format. 我需要做的是将每个字母读入一个数组字母,每个字母用空行分隔,并且字母必须遵循这种格式。 It's a 5x9 matrix of numbers that I need to convert into a 45 x 1 array and store that in an alphabet array of 26 letters. 这是一个5x9的数字矩阵,我需要将其转换为45 x 1的数组并将其存储在26个字母的字母数组中。

This is for an optical character recognition neural network that i've got to work with hard coded numbers but reading from a file for the data has proven trick. 这是用于光学字符识别神经网络的,我必须使用硬编码数字,但是从文件中读取数据已被证明是技巧。

This is what I have so far: 这是我到目前为止的内容:

String[][] alphabet = new String[26][45];
    float [][] trainingDataFile = new float[26][45];
    int row = 0;

    Scanner file = new Scanner(new BufferedReader(new FileReader("Alphabet.txt")));
    /*
        While the file has another line, read in data until empty line. 
    */
    while(file.hasNextLine())
    {

        String line = file.nextLine();
        if(line.length() != 0)
        {
            String[] letters = line.split(" ");
            alphabet[row] = letters;

        } else {
            row++;
        }

    }

In my head the algorithm would go: Read in data and append to string until empty line then increment to next letter. 在我的脑海中,算法将去:读入数据并追加到字符串,直到空行,然后递增到下一个字母。

But I cannot work out how to translate that into code. 但是我不知道如何将其转换为代码。 I can't seem to figure out how to keep reading the block of a single letter until an empty line. 我似乎无法弄清楚如何继续读取单个字母的块直到空行。

  1. The file is small enough that it can be loaded into memory. 该文件足够小,可以将其加载到内存中。 Java 7 nio has a one line method that does that. Java 7 nio有一个单行方法可以做到这一点。

  2. It much easier to work with List s than arrays, since they automatically grow as data is inserted. 使用List比使用数组要容易得多,因为它们会随着插入数据而自动增长。 you convert arrays to list and vice versa as needed. 您可以根据需要将数组转换为列表,反之亦然。

Here is my solution: 这是我的解决方案:

    String[][] alphabet = new String[26][45];

    try {
        // read the entire file into memory
        List<String> lines = Files.readAllLines(Paths.get("C://temp/xx.txt"));
        // this will hold 45x1 array as list
        List<String> concatenated = new ArrayList<>();
        int row = 0;
        for (String line : lines) {
            if (line.isEmpty()) {
                // convert list to array and add to matrix
                alphabet[row] = concatenated.toArray(alphabet[row]);
                concatenated = new ArrayList<>();
                row++;
            } else {
                // convert result of split() to list and add to letter list
                concatenated.addAll(Arrays.asList(line.split(" ")));
            }
        }
        // take care of last letter
        alphabet[row] = concatenated.toArray(alphabet[row]);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Arrays.stream(alphabet).forEach(row -> System.out.println(Arrays.toString(row)));
}

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

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