I have a problem when parsing a string to double after read from a text file. This is my .txt file:
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
In my code, the first line I read, just takes n=5 and m=2. From the second line to the end, I just use the first and the second value as index of a matrix, and the third value is a double, which I want to write in the position of the array given by the first and the second value.
When I read a line, I parse to integer the first two values, and the third one to double.
the problem I am having is that when I split the line with a space as separator (" "), I can get the first and the second value correctly, but I am getting an error with the third one when I try to convert it from String to double. Here is the code:
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();
}
}
In this line:
double enlace = Double.parseDouble(split[2]);
I am getting the error "Source not found", and I cannot understand why. When I access to this position of the split, it has value inside. Also, in this same line, if I write:
double enlace = Double.parseDouble(split[1]);
instead of split[2], the program runs with no errors. I think there is something wrong in this split line, but I cannot see what.
Maybe someone can help me because I am not able to see what is wrong here..
Thank you so much.