java - Reading a text file into a 3d array -


I want to read my content in a text file in a multi-dimensional array My text file contains a string in a specific format.

I want to store it in a multi-dimensional array I tried to use the Java code to store in a string or string array but I do not know how to format this format in a multidimensional array Have to store. Can anyone help me?

My text file is in read.txt

  {{"Kim", "is" "game"}, "{" "NIEAN "{{" "," Is "," life "," in "," in "," DEET "" VERB "}}, {{" Kevin "}, {" NEUN "," VERB "," DEET "}} What I tried is:  
      

Code> Public Class Mayclass {Stable Last String [] [] [] MULTI_ARRAY = New String [] [] []; Public static zero (string [] [] [string] [for] mary: muli_ARRAY] string [] sentences = maryare [0]; string [] partofespitch = myrobalan [1];}}} bufffind reader br = new bufted Reader (New Flamerdder ("Reading .txt"); while (Bretty) {multi_array [] [] [] = br.readLine ();}}

i.e. The last string in the following format

  Fixed last string [] [] [] MULTI_ARRAY = new string [] [] [] {{{"Kim", " "" Game "}, {" NUN "," VERB "," Deight "{}" {{"Shine", "is" "eat"}, {"NUN", "DET" "VERB"}}, {{"Kevin", "life", "in", "holland"} , {"NUN", "VERB", "DET", "Holland"}}}  

Let's assume that the format is 1 2D blocks per line, apart from that, I used arrays instead of s because you do not know the required size in advance. :

  Scanner scan = new scanner (new file ("file.txt")); & Lt; From the list & lt; From the list & lt; String & gt; & Gt; & Gt; D3 = New Arrestist & lt; & Gt; (); While (scan.hasNextLine ()) {string line = scan. NXtine (); String [] splitted = line.split ("(? & Lt;! \\\\) \\\" "); list & lt; list & lt; string & gt; & gt; d2 = new arreelist & lt; & Gt; (); d3.add (d2); List & lt; String & gt; D1 = New Arrielast & lt; & gt; (); d2.add (d1); // to ignore first and Last (int i = 1; i & lt; splitted.length - 1; i ++) {if ((I & amp; 1)! = 0) {// Strange, add / unescape double quotes and backslash d1 to the list .add (splitted [i] .replace ("\\\", "\" "). (". \ "," \\ "));} Other {// test even if the new array starts (Tied [i] .matches (". * \\ {. *")) {D1 = new ArrayList & lt; & Gt; (); D2.add (d1);}}}} scan.close (); System.out.println (d3);  

This idea is based on partition by ", however, if you avoid them ( \" ) and Thanks to this, split correctly, you can still keep quotes within your wires. But it fail if E has backslash string nd of string

Edit: List & lt; List & lt; List & lt; String & gt; & Gt; & Gt; You can use this code:

  string [] [/ s] ] [] Results = new string [d3.size ()] [] []; For (int i = 0; i & lt; result.length; i ++) {result [i] = new string [d3.get (i). Shape ()] []; (Int j = 0; j & lt; result [i] .length; j ++) {result [i] [j] = new string [d3.get (i) .get (j) .ize () ]; (Int k = 0; k & lt; result [i] [j] .length; k ++) {result [i] [j] [k] = d3.get (i) .get (j). Get (k); }}} System.out.println (Arrays.deepToString (Results)); // See that the result is OK  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -