java - read file, convert string to double, store in 2d array -
i need read list of numbers in file , store 2d array. have far. how go achieving goal?
//this part of code public class rainfall { double[][] precip; public rainfall() { precip = new double [5][12]; } public void readfile(bufferedreader infile) throws ioexception { fileinputstream infile = new fileinputstream("numbers.dat"); bufferedreader br = new bufferedreader(new inputstreamreader(infile)); string[][] myarray = new string[5][12]; while (infile.readline() != null) { (int j = 0; j < 5; j++) { (int = 0; < 12; i++) { myarray[j][i] = infile.readline(); } } } infile.close(); }
numbers.dat 60 lines of:
1.01
0.03
2.14
0.47
//is each number on new line? you're close, added few lines below. public class rainfall { double[][] precip; public rainfall() { precip = new double [5][12]; } public void readfile(bufferedreader infile) throws ioexception { //fileinputstream infile = new fileinputstream("numbers.dat"); bufferedreader br = new bufferedreader(new filereader("numbers.dat")); string line = ""; string[][] myarray = new string[5][12]; while ((line = br.readline()) != null) { double num = double.parsedouble(line.trim()); (int j = 0; j < 5; j++) { (int = 0; < 12; i++) { precip[j][i] = num; } } } br.close(); }
Comments
Post a Comment