I am trying to read a given text file filled with a bunch of doubles, the text file looks something like this(no spaces between each line):
0, 0.007133248, 0.003747135, 0.0034464097, 0.009517824, 0.0036065334, 0.007921185, 0.0041013476
1, 0.006223865, 5.804103E-4, 5.6967576E-4, 0.008850083, 0.003269921, 3.7322243E-4, 0.0011008179
2, 0.0051101227, 0.008973722, 0.0013274436, 0.00922496, 0.0050817304, 0.004631279, 0.0069321943
essentially 1000 rows with 8 columns, and am trying to turn this into a 2d array of data[1000][8], am having trouble iterating through the data though. Heres what I have so far, any help would be appreciated!
public static void readFile2() throws IOException{
Double[][] data = new Double[1000][8];
int row = 0;
int col = 0;
Scanner scanner = null;
scanner = new Scanner(new BufferedReader(new FileReader("/Users/Roman/Documents/workspace/cisc124/Logger (1).csv")));
while (scanner.hasNext() && scanner !=null) {
scanner.useDelimiter(",");
while(row<data.length && col<8){
data[row][col] = Double.parseDouble(scanner.next());
col++;
//System.out.print(Arrays.deepToString(data));
}
col=0;
row++;
}
System.out.println(Arrays.deepToString(data));
scanner.close();
}
It would be more convenient reading thefile line by line..
String[] data = new String[1000];
int row = 0;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "utf-8"));
String line;
while ((line = br.readLine()) != null) {
data[row]= line;
row++;
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
and at the end do the same
System.out.println(Arrays.ToString(data));
or if you need the element at a given index then split a row and use Double.parse();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.