繁体   English   中英

txt文件转换为双二维数组

[英]Txt file into a double 2d array

我正在尝试将txt文件读入双打数组。 我正在使用以下代码来读取文件的每一行:

String fileName="myFile.txt";
try{

    //Create object of FileReader
    FileReader inputFile = new FileReader(fileName);

    //Instantiate the BufferedReader Class
    BufferedReader bufferReader = new BufferedReader(inputFile);

    //Variable to hold the one line data
    String line;

    // Read file line by line and print on the console
    while ((line = bufferReader.readLine()) != null)   {

        System.out.println(line);

    }

    //Close the buffer reader
    bufferReader.close();

}catch(Exception e){
    System.out.println("Error while reading file line by line:" 
        + e.getMessage());                      
}

但是我想将txt文件存储到二维双数组中。 我已经尝试了上述方法来加载txt的尺寸。 但是我在异常捕获(NoSuchElementException e)上遇到了问题,看来它无法读取文件。

try {

        while (input.hasNext()) {

            count++;
            if (count == 1) {

                row = input.nextInt();
                r = row;
                System.out.println(row);
                continue;
            } else if (count == 2) {
                col = input.nextInt();
                System.out.println(col);
                c = col;
                continue;
            } else {

                output_matrix = new double[row][col];

                for (int i = 0; i < row; i++) {
                    for (int j = 0; j < col; j++) {

                        String el = input.next();
                        Double temp = Double.valueOf(el);
                        double number = temp.doubleValue();

                        //output_matrix[i][j] = el;
                        output_matrix[i][j] = number;
                        //System.out.print(output_matrix[i][j]+" ");
                    }
                    //System.out.println();
                }
            }
        }

    } catch (NoSuchElementException e) {
        System.err.println("Sfalma kata ti tropopoisisi toy arxeioy");
        System.err.println(e.getMessage());       //emfanisi tou minimatos sfalmatos
        input.close();
        System.exit(0);
    } catch (IllegalStateException e) {
        System.err.println("Sfalma kata ti anagnosi toy arxeioy");
        System.exit(0);
    }

您可能想要为其使用Scanner类,尤其是Scanner.nextDouble()方法。

另外,如果您事先不知道数组的尺寸-我建议您使用ArrayList而不是常规数组。

代码示例:

ArrayList<ArrayList<Double>> list = new ArrayList<>();
while ((line = bufferReader.readLine()) != null)   {
    ArrayList<Double> curr = new ArrayList<>();
    Scanner sc = new Scanner(line);
    while (sc.hasNextDouble()) {
        curr.add(sc.nextDouble());
    }
    list.add(curr);
}

在fir处声明一个列表并收集所有已读行:

List<String> tempHistory = new ArrayList<>();
while ((line = bufferReader.readLine()) != null) {
    tempHistory.add(line);
}

然后,在bufferReader.close();之后bufferReader.close(); 将此tempHistory列表转换为double[][]数组。

double[][] array = new double[tempHistory.size()][];
for (int i = 0; i < tempHistory.size(); i++) {
    final String currentString = tempHistory.get(i);
    final String[] split = currentString.split(" ");
    array[i] = new double[split.length];
    for (int j = 0; j < split.length; j++) {
        array[i][j] = Double.parseDouble(split[j]);
    }
}

它可以工作,但是正如我在注释中添加的那样,这不是一个很好的解决方案,最好使用Collections而不是array

顺便说一句,即使不同行的行长不同,它也可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM