首页 > 解决方案 > 爪哇;无法将字符串矩阵转换为双矩阵:Errorjava.lang.NumberFormatException

问题描述

3天前我试图解决这个问题,但没有任何反应。我希望你能找到解决办法:

我正在制作一个应用程序来读取一个数字的 txt 文件,然后我将数据捕获到一个字符串矩阵中并操作它们我需要将它转换为一个双矩阵。但出现错误:

Errorjava.lang.NumberFormatException:对于输入字符串:“0,95768412 0,770070937”

. 我试图将逗号 (.) 替换为 (.) 但没有发生任何事情。

Here a thumbnail of the file info:
0,620966467 0,397670717
0,144506398 0,86070719
0,344924707 0,49886148
0,568299164 0,407224505 0,55644466
0,580297755
0,940100947 0,920269925
0,45667026 0,253952562
0,046970841 0,04214613
0,548769197 0,114155205
0,220420195 0,035404045
0,804653981
0,371228693 0,688345818
0,575316752 0,914647,

我发布代码,你可以看到程序。

try {

        BufferedReader br = new BufferedReader(new FileReader("src\\numerosAleatorios.txt"));

        //String matriz[][] = new String[99][1];
        double matriz[][] = new double[99][2];
        int numlineas = 0;
        while (((Linea = br.readLine()) != null)) {

            String a[] = Linea.split(" ");

            for (int i = 0; i < a.length; i++) {

                matriz[numlineas][i] = Double.parseDouble(a[i]);

            }

            numlineas++;
        }

        //double matrizDoble[][]= new double [99][1];
        System.out.println("MATRIZ");
        System.out.println("------------------------------");

        for (int filas = 0; filas < matriz.length; filas++) {
            for (int colum = 0; colum < matriz[filas].length; colum++) {
                //matrizDoble[filas][colum]= Double.valueOf(matriz[filas][colum]).doubleValue();

                System.out.print(matriz[filas][colum] + "\n");

            }  
        }



            System.out.println("\n Numero de parejas: "+numlineas);
    } catch (Exception ex) {
        System.out.println("Error"+ex);
    }

谢谢你的回答。!

标签: javamatrix

解决方案


我已尝试将您的代码替换为“。” 它对我来说很好。

在此处输入图像描述

在此处输入图像描述

String Linea;
try {

    BufferedReader br = new BufferedReader(new FileReader("src\\numerosAleatorios.txt"));

    // String matriz[][] = new String[99][1];
    double matriz[][] = new double[99][2];
    int numlineas = 0;
    while (((Linea = br.readLine()) != null)) {

        String a[] = Linea.split(" ");

        for (int i = 0; i < a.length; i++) {

            matriz[numlineas][i] = Double.parseDouble(a[i]);

        }

        numlineas++;
    }

    // double matrizDoble[][]= new double [99][1];
    System.out.println("MATRIZ");
    System.out.println("------------------------------");

    for (int filas = 0; filas < matriz.length; filas++) {
        for (int colum = 0; colum < matriz[filas].length; colum++) {

            System.out.print(matriz[filas][colum] + "\n");

        }
    }

    System.out.println("\n Numero de parejas: " + numlineas);
} catch (Exception ex) {
    System.out.println("Error" + ex);
}

推荐阅读