java - 拆分和隐蔽以使从文本文件中读取的值加倍时找不到源错误
问题描述
从文本文件读取后将字符串解析为双精度时出现问题。这是我的 .txt 文件:
5 2
0 1 166.47234
0 2 170.18475
0 3 174.55453
0 4 153.28670
1 2 145.12186
1 3 144.42723
1 4 170.98466
2 3 176.58110
2 4 162.99632
3 4 168.48360
在我的代码中,我阅读的第一行只需要 n=5 和 m=2。从第二行到最后,我只是用第一个和第二个值作为矩阵的索引,第三个值是一个double,我想写在第一个和第二个值给定的数组的位置.
当我读到一行时,我将前两个值解析为整数,第三个值加倍。
我遇到的问题是,当我用空格作为分隔符(“”)分割行时,我可以正确获取第一个和第二个值,但是当我尝试将其从字符串加倍。这是代码:
static File file_ = new File("C:\\Users\\dlozanoe\\Desktop\\Personal\\Universidad\\2o Semestre\\Tendencias en Inteligencia Artificial\\Tema 3\\Datos.txt");
static int n_localizaciones = 0;
static int m = 0;
static int contador = 0;
static int fila = 0;
static int columna = 0;
static double enlace = 0;
double mejorValorFuncionObjetivo;
static double matriz[][];
static String split[];
static boolean seleccion[];
public static void main(String[] args) throws IOException {
LeerMatrizArchivo();
for (int i = 0; i < n_localizaciones; i++) {
for (int j = 0; j<n_localizaciones; j++) {
System.out.print(matriz[i][j] + "\t");
}
System.out.println();
}
}
private static void LeerMatrizArchivo() throws IOException {
BufferedReader in = new BufferedReader(new FileReader(file_));
try {
String line = in.readLine();
split = line.split(" ");
n_localizaciones = Integer.parseInt(split[0]);
m = Integer.parseInt(split[1]);
matriz = new double [n_localizaciones][n_localizaciones];
line = in.readLine();
while (line != null) {
split = line.split(" ");
fila = Integer.parseInt(split[0]);
columna = Integer.parseInt(split[1]);
double enlace = Double.parseDouble(split[2]);
matriz[fila][columna] = enlace;
matriz[columna][fila] = enlace;
line = in.readLine();
}
} catch (FileNotFoundException ex){
} finally {
in.close();
}
}
在这一行:
double enlace = Double.parseDouble(split[2]);
我收到错误“找不到源”,我不明白为什么。当我访问拆分的这个位置时,它在里面是有价值的。另外,在同一行中,如果我写:
double enlace = Double.parseDouble(split[1]);
而不是 split[2],程序运行没有错误。我认为这条分割线有问题,但我看不出是什么。
也许有人可以帮助我,因为我看不到这里有什么问题..
太感谢了。
解决方案
更改此行:
double enlace = Double.parseDouble(split[2]);
至:
double enlace = Double.parseDouble(split[2].replace(".", ","));
问题是由于您的电脑的本地设置,.
您必须将其更改为点。,
推荐阅读
- ios - 删除 List Swift UI Xcode 11 中的空行
- python - Python 处理打开另一个工具的 cmd 命令
- ios - 如何在一个视图控制器中制作单独的选取器视图?
- openapi - Stoplight Prism 找不到 text/html 的序列化程序
- python-2.7 - 如何运行鸡蛋分发的pyspark应用程序?
- android - 如何根据firestore中的hashmap值过滤项目
- android - 如何在Android的WebView中解码符号'?
- java - 将使用静态方法的测试类转换为服务 Spring Boot?
- bash - Shell 参数扩展在 ksh 中有效,但在 bash 中无效
- c# - C# Powershell Commandlet 使用两个参数,但最多可以指定一个