3天前,我试图解决此问题,但没有任何反应。希望您能找到解决方案:
此后,我正在制作一个应用程序来读取数字的txt文件,我将数据捕获到字符串矩阵中并进行操作,我需要将其转换为双精度矩阵。但出现错误:
。我尝试用逗号(。)替换(。),但没有发生。
以下是文件信息的缩略图:
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,575313752
0,54377148 0,891464466
我发布代码以供您查看程序。
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);
}
感谢您的回答!!
答案 0 :(得分:1)
我已尝试将您的代码替换为“,”。对我来说很好。
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);
}
答案 1 :(得分:0)
1)用空格分割字符串:
String a[] = Linea.split("\\s+");
2)将“,”替换为“。” :
matriz[numlineas][i] = Double.parseDouble(a[i].replace(",", "."));