Home > OS >  read txt file of ascii chars into 2d array in java
read txt file of ascii chars into 2d array in java

Time:10-02

I have txt file consists of ascii image (sample shown below) that I am trying to pass into a 2d array of unknown size which means I do not know the size of [row] and [col].

I have found so many things but none have worked for me.

Here

Here

Here

I have tried to adapt the things I read to make it work for my code but no progress. Please consider helping. Note I am doing this inside a class AsciiImage.

ArrayList<String>[][] arrayOfCharacters = new ArrayList[20][20];

    

    public AsciiImage(String passFile) throws FileNotFoundException, IOException{
         try{
                Scanner input = new Scanner(new File(passFile));

                int row = 0;
                int column = 0;

                while(input.hasNext()){
                    String[] c = input.nextL();
                    arrayOfCharacters[row][column] = c.charAt(0);

                    column  ;

                    // handle when to go to next row
                }   

                input.close();
            } catch (FileNotFoundException e) {
                System.out.println("File not found");
                // handle it
            }
        

Here is the txt file I am working on.

                .'|     .8
               .  |    .8:
              .   |   .8;:        .8
             .    |  .8;;:    |  .8;
            .     n .8;;;:    | .8;;;
           .      M.8;;;;;:   |,8;;;;;
          .    .,"n8;;;;;;:   |8;;;;;;
         .   .',  n;;;;;;;:   M;;;;;;;;
        .  ,' ,   n;;;;;;;;:  n;;;;;;;;;
       . ,'  ,    N;;;;;;;;:  n;;;;;;;;;
      . '   ,     N;;;;;;;;;: N;;;;;;;;;;
     .,'   .      N;;;;;;;;;: N;;;;;;;;;;
    ..    ,       N6666666666 N6666666666
    I    ,        M           M
   ---nnnnn_______M___________M______mmnnn
         "-.                          /
 ~~~~~~~~~~~@@@**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

CodePudding user response:

I created an input file with your ASCII boat and was able to reproduce the image.

                .'|     .8
               .  |    .8:
              .   |   .8;:        .8
             .    |  .8;;:    |  .8;
            .     n .8;;;:    | .8;;;
           .      M.8;;;;;:   |,8;;;;;
          .    .,"n8;;;;;;:   |8;;;;;;
         .   .',  n;;;;;;;:   M;;;;;;;;
        .  ,' ,   n;;;;;;;;:  n;;;;;;;;;
       . ,'  ,    N;;;;;;;;:  n;;;;;;;;;
      . '   ,     N;;;;;;;;;: N;;;;;;;;;;
     .,'   .      N;;;;;;;;;: N;;;;;;;;;;
    ..    ,       N6666666666 N6666666666
    I    ,        M           M
   ---nnnnn_______M___________M______mmnnn
         "-.                          /
 ~~~~~~~~~~~@@@**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I used a List<list<Character>> to hold the ASCII matrix image.

I created two methods. The createImageMatrix method creates the List<List<Character>> image matrix. Each line of the image text file is read and put in a List<Character> line list. Each line list is put in the image matrix.

The printImageMatrix method uses a StringBuilder to construct the ASCII image for display and verification.

Here's the complete runnable code. You can ignore the first two lines of the main method. I keep all my testing resources in a resource folder. The first two lines give me the complete path to the text file in the resource folder.

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class AsciiImage {

    public static void main(String[] args) {
        URL url = AsciiImage.class.getResource("/boat.txt");
        String filename = url.getFile();

        AsciiImage ai = new AsciiImage(filename);
        List<List<Character>> imageMatrix = ai.createImageMatrix();
        System.out.println(ai.printImageMatrix(imageMatrix));
    }

    private String filename;

    public AsciiImage(String filename) {
        this.filename = filename;
    }

    public List<List<Character>> createImageMatrix() {
        List<List<Character>> imageMatrix = new ArrayList<>();

        try {
            Scanner input = new Scanner(new File(filename));
            while (input.hasNext()) {
                String s = input.nextLine();
                List<Character> lineList = new ArrayList<>(s.length());
                for (int index = 0; index < s.length(); index  ) {
                    lineList.add(Character.valueOf(s.charAt(index)));
                }
                imageMatrix.add(lineList);
            }
            input.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return imageMatrix;
    }

    public String printImageMatrix(List<List<Character>> imageMatrix) {
        StringBuilder builder = new StringBuilder();
        for (List<Character> lineList : imageMatrix) {
            for (char c : lineList) {
                builder.append(c);
            }
            builder.append(System.lineSeparator());
        }

        return builder.toString();
    }

}
  • Related