Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- public class FileParser {
- public float[][] getMatrixFromFile(String filename) throws IOException {
- Stream<String> stream = Files.lines(Paths.get(filename));
- List<String> lines = stream.collect(Collectors.toList());
- int size = getMatrixSize(lines.get(0));
- float[][] matrix = new float[size][size+1];
- int row = 0;
- int col = 0;
- for (String line : lines.subList(1, lines.size() - 1)) {
- for (String s : line.split(" ")) {
- try {
- matrix[row][col] = Float.parseFloat(s);
- col += 1;
- } catch (NumberFormatException e) {}
- }
- row += 1;
- col = 0;
- }
- fillLastColumn(matrix, lines.get(lines.size() -1));
- return matrix;
- }
- private int getMatrixSize(String line) {
- return Integer.parseInt(line);
- }
- private void fillLastColumn(float[][] matrix, String line) {
- int row = 0;
- int col = matrix.length;
- for (String s : line.split(" ")) {
- matrix[row][col] = Float.parseFloat(s);
- row += 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement