java - Detect an array from text file -
i have files containing text, have extract information, among have 2-dimensional double array(sometimes might missing - therefore you'll find "if" clause).
this way file formatted:
name=filename
groups={ group1=groupname group2=groupname minage= maxage= ages=[[18.0,21.0,14.7],[17.3,13.0,12.0]] }
i using java.nio.file.files, java.nio.file.path , java.io.bufferedreader read these files, having problems while trying convert strings representing arrays real java arrays:
path p = paths.get(filename); try(bufferedreader br = files.newbufferedreader(p)) { string line = br.readline(); string filename = line.split("=")[1]; line = br.readline(); string[] arr = line.split("="); string group1 = arr[2].split(" ")[0]; string group2 = arr[3].split(" ")[0]; integer minage = integer.parseint(arr[4].split(" ")[0]); integer maxage = integer.parseint(arr[5].split(" ")[0]); double[][] ag = null; if (line.contains("ages")) { string age = arr[6].trim().replace("}", "").replace("[[", "").replace("]]", "").trim(); string[] arrage = weights.split(","); //don't know here on, since number of arrays inside //the first 1 may vary 1 2 (e.g might find: [[3.0, 4.0]] or [[3.0, 7.0],[4.0,5.0]]) //this trying ag = new double[1][arrage.length]; (int = 0; < arrage.length; i++) ag[0][i] = double.parsedouble(arrage[i]); } } catch (exception e) { e.printstacktrace(); }
is there way detect array text without doing trying in code or there way extract correct 2-dimensional array reading file formatted way?
one more question: there way print 2-dimensional array that? if yes, how? (by using arrays.tostring this: [[d@69222c14])
you can use regex extract 2 dimensional array string.
string groups="{ group1=groupname group2=groupname minage= maxage= ages=[[18.0,21.0,14.7],[17.3,13.0,12.0]] }"; string pattern = "(\\[\\[.+\\]\\])"; pattern r = pattern.compile(pattern); matcher m = r.matcher(groups); if (m.find( )) system.out.println(m.group());
the output above code is:
[[18.0,21.0,14.7],[17.3,13.0,12.0]]
Comments
Post a Comment