Java read values from text file -
i new java. have 1 text file below content.
`trace` - structure( list( "a" = structure(c(0.748701,0.243802,0.227221,0.752231,0.261118,0.263976,1.19737,0.22047,0.222584,0.835411)), "b" = structure(c(1.4019,0.486955,-0.127144,0.642778,0.379787,-0.105249,1.0063,0.613083,-0.165703,0.695775)) ) )
now want is, need "a" , "b" 2 different array list.
you need read file line line. done bufferedreader
:
try { fileinputstream fstream = new fileinputstream("input.txt"); bufferedreader br = new bufferedreader(new inputstreamreader(fstream)); string strline; int linenumber = 0; double [] = null; double [] b = null; // read file line line while ((strline = br.readline()) != null) { linenumber++; if( linenumber == 4 ){ = getdoublearray(strline); }else if( linenumber == 5 ){ b = getdoublearray(strline); } } // close input stream in.close(); //print contents of for(int = 0; < a.length; i++){ system.out.println("a["+i+"] = "+a[i]); } } catch (exception e) {// catch exception if system.err.println("error: " + e.getmessage()); }
assuming "a"
and"b"
on fourth , fifth line of file, need call method when these lines met return array of double
:
private static double[] getdoublearray(string strline) { double[] a; string[] split = strline.split("[,)]"); //split line @ ',' , ')' characters = new double[split.length-1]; for(int = 0; < a.length; i++){ a[i] = double.parsedouble(split[i+1]); //get double value of string } return a; }
hope helps. still highly recommend reading java i/o , string tutorials.
Comments
Post a Comment