简体   繁体   中英

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:

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

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.

  2. It much easier to work with List s than arrays, since they automatically grow as data is inserted. 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)));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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