'Split special type of contents of a file into a 2D array

I have a text file, whose contents look something like this:

XXXX
X  X
X  X
XXXX

I do not know the actual dimensions of the text file. I am trying to write a program that will read the text file and split each line of the file.

This is the code I have so far:

import java.util.*;
import java.io.*;

public class MyClass {
    public static void main(String args[]) {
        String[] line = {};
        String[] splitLine = {};
        
        Scanner scan = new Scanner(new File("somefile.txt"));
        while (scan.hasNextLine()){
            line = Arrays.copyOf(line, line.length + 1);
            line[line.length-1] = scan.nextLine();
        }
        
        for(int i = 0; i < line.length; i++){
            splitLine = line[i].split("((?<=\\s+)|(?=\\s+))");
            for(int j = 0; j < splitLine.length; j++){
                if(splitLine[j].equals("X")){
                    System.out.print("A");
                } else if(splitLine[j].equals(" ")){
                    System.out.print("B");
            }
        }
        System.out.println("");
        System.out.println("Done!");
    }
}

The array I want is:

{{"X", "X", "X", "X"}, 
{"X", " ", " ", "X"}, 
{"X", " ", " ", "X"}, 
{"X", "X", "X", "X"}}

And therefore the output this code should give me is:

AAAAABBAABBAAAAA
Done!

How would I fix this code to achieve what I need?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source