0

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
         "-.                          /
 ~~~~~~~~~~~@@@**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
spock's-neurons
  • 79
  • 2
  • 11
  • You can store the ASCII characters from one line in a List and store each line in a List>. – Gilbert Le Blanc Oct 02 '21 at 03:12
  • Would you mind elaborating more! If I knew how to do that I would not be asking the questions here. – spock's-neurons Oct 02 '21 at 03:31
  • You're using an ArrayList (incorrectly), not a 2D array. In order to use a 2D array, you'd have to read the input file twice. Once to get the line count for the first array limit, and again to get the second array limit and put the characters in the array. – Gilbert Le Blanc Oct 02 '21 at 03:31
  • so if I understand you correctly my code should look like this but I am still get error of miss match saying cant convert from string to char `` while(input.hasNext()){ for(int i =0; i < myTwoDd.length; i++) { String[] line = input.nextLine().split(""); for(int j =0; j – spock's-neurons Oct 02 '21 at 03:52

1 Answers1

1

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();
    }

}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111