Adding lines from a file into 2D array Java -
this question has answer here:
- read .txt file 2d array 4 answers
i'm having trouble trying write syntax in java want file
what i'm doing reading in file , want add specific lines of file elements of 2d array, have written pseudo code below understand im trying
try { fileinputstream fstream = new fileinputstream(filename); datainputstream instream = new datainputstream(fstream); bufferedreader brread = new bufferedreader (new inputstreamreader(instream)); string line; while ((line = brread.readline()) !=null) { //lines 1-3 //{ // add line 1 element 0,0 of array // add line 2 element 0,1 of array // add line 3 element 0,2 of array //} //lines 4-10 //{ // add line 4 element 1,0 of array // add line 5 element 1,1 of array // ect.. //} } } catch (exception e) { } }
suppose 2d arrays 3 x 3 (not explained clearly.) algorithm , code similar below, quite simple.
try { fileinputstream fstream = new fileinputstream(filename); datainputstream instream = new datainputstream(fstream); bufferedreader brread = new bufferedreader (new inputstreamreader(instream)); string line; int linecount = 0; int mod9 = -1; int firstindex = -1; int secondindex = -1; double[][] array = new double[3][3]; while ((line = brread.readline()) !=null) { //lines 1-3 //{ // add line 1 element 0,0 of array // add line 2 element 0,1 of array // add line 3 element 0,2 of array //} mod9 = linecount % 9 ; firstindex = mod9 / 3; secondindex = mod9 % 3; array[firstindex] += double.parsedouble(line); linecount++; //lines 4-10 //{ // add line 4 element 1,0 of array // add line 5 element 1,1 of array // ect.. //} } } catch (exception e) { }
Comments
Post a Comment