Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a text file into a 3d array

I want to read my content of a text file into a multidimensional array. My text file consists of strings in a specific format.

I just want to store it into a multidimensional array. I tried using java code to store into string or string array but I don't know how to store this format into a multidimensional array. Can any one help me?

my text file read.txt consists of

    { { "Kim","is" "playing" },         { "NOUN", "VERB", "DET" } },
    { { "Shine","is" "eating"},         { "NOUN","DET" "VERB" } },
    { { "Kevin","lives","in","Holland"},  { "NOUN", "VERB ","DET","Holland"} }

what I tried is :

    public class MyClass
    {
      static final String[][][] MULTI_ARRAY = new String[][][];
      public static void main(String args[]
      {
        for (String[][] myarray : MULTI_ARRAY) 

            String[] sentences = myarray[0];
            String[] partsofspeech = myarray[1];  
        }
      }}

      BufferedReader br=new BufferedReader(new FileReader("read.txt"));
      while(br.ready)
      {
         MULTI_ARRAY[][][]=br.readLine();

      }
   }

ie I want my 3d array in the below format

    static final String[][][] MULTI_ARRAY = new String[][][] {
   { { "Kim","is" "playing" },         { "NOUN", "VERB", "DET" } },
    { { "Shine","is" "eating"},         { "NOUN","DET" "VERB" } },
    { { "Kevin","lives","in","Holland"},  { "NOUN", "VERB ","DET","Holland"} }
  };
like image 535
chopss Avatar asked Aug 03 '26 00:08

chopss


1 Answers

Let's assume than the format is 1 2D block per line. Also I used ArrayLists instead of arrays, since you don't know the needed sizes in advance. Here is my code:

Scanner scan = new Scanner(new File("file.txt"));

List<List<List<String>>> d3 = new ArrayList<>();

while (scan.hasNextLine()) {
    String line = scan.nextLine();

    String[] splitted = line.split("(?<!\\\\)\\\"");
    List<List<String>> d2 = new ArrayList<>();
    d3.add(d2);

    List<String> d1 = new ArrayList<>();
    d2.add(d1);

    // ignore first and last
    for (int i = 1; i < splitted.length - 1; i++) {
        if ((i & 1) != 0) { // odd, add to list
            // unescape double quote and backslash
            d1.add(splitted[i].replace("\\\"", "\"").replace("\\\\", "\\"));
        } else { // even test if new array starts
            if (splitted[i].matches(".*\\{.*")) {
                d1 = new ArrayList<>();
                d2.add(d1);
            }
        }
    }
}

scan.close();
System.out.println(d3);

The idea is based on splitting the line by the " characted, however, you still can have quotes inside your strings if you escape them (\") and thanks to negative lookbefore it will split correctly. But it will fail if there is backslash at the end of the string.

EDIT:

To convert from List<List<List<String>>> to String[][][] you can use this code:

String[][][] result = new String[d3.size()][][];
for (int i = 0; i < result.length; i++) {
    result[i] = new String[d3.get(i).size()][];
    for (int j = 0; j < result[i].length; j++) {
        result[i][j] = new String[d3.get(i).get(j).size()];
        for (int k = 0; k < result[i][j].length; k++) {
            result[i][j][k] = d3.get(i).get(j).get(k);
        }
    }
}

System.out.println(Arrays.deepToString(result)); //see if result is ok
like image 92
kajacx Avatar answered Aug 04 '26 13:08

kajacx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!