java - 爪哇;无法将字符串矩阵转换为双矩阵: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);
}
谢谢你的回答。!
解决方案
我已尝试将您的代码替换为“。” 它对我来说很好。
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);
}
推荐阅读
- python - 使用列表列表在 pandas 中创建 DataFrame
- reactjs - 在 ReactJS 中将 props 传递给组件
- c# - C# OrderBy 方法如何按包含哈士奇代码的字符对对象进行排序?
- java - 尝试创建一个活动,当单击字体按钮时,fontfamily 应该更改
- reactjs - 如何解决“无效的 Hook Call”?
- html - 使用 HTML 时标记为属性指定值而不为属性指定名称的结果是什么?见描述中的例子
- java - 同时使用 2 个用户界面(GUI 和控制台)中断 Scanner.readLine 调用
- java - 如何在过滤器中从数据库中获取用户并在 jwt 响应中添加 ID?
- python - python中的有序排列
- python - 运行此程序时,我不断收到此错误。我已经尝试了我能想到的一切,但仍然无法正常工作