Loading two-dimensional board

markiewicz36 :

i have a problem with this array:

1.823803 1.000000 
2.214117 1.000000 
4.356716 1.000000 
4.455463 1.000000 
3.467892 1.000000 
2.480369 1.000000 
3.273540 1.000000 
3.281274 1.000000 
0.205179 1.000000 
2.103515 1.000000 
-0.057308 1.000000 
1.794524 1.000000 
3.160924 2.000000 
2.856910 1.000000 
2.247974 2.000000 
1.953566 1.000000 
4.241937 1.000000 
1.782172 1.000000 
4.869065 1.000000 
2.090794 1.000000 
1.663878 1.000000 
3.157155 1.000000 
3.501306 1.000000 
2.066036 1.000000 
4.793069 1.000000 
2.484362 1.000000 
2.201043 2.000000 
4.189059 1.000000 

I have to load it from a file to two single boards or one two dimensional board. I don't know how to do it. They have to divide it into 1 and 2. Anybody want to help me?

oleg.cherednik :

There're many ways to read text file in Java. One of them is to use Scanner:

public static double[][] readTableFromFile(File file) throws FileNotFoundException {
    try (Scanner scan = new Scanner(file)) {
        scan.useLocale(Locale.US);

        List<Double> data = new ArrayList<>();

        while (scan.hasNextDouble())
            data.add(scan.nextDouble());

        double[][] table = new double[data.size() / 2][2];
        Iterator<Double> it = data.iterator();
        int row = 0;

        while (it.hasNext()) {
            table[row][0] = it.next();
            table[row++][1] = it.next();
        }

        return table;
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=403784&siteId=1