0

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.

daniel lozano
  • 421
  • 6
  • 19
  • You should follow the **Java Naming Conventions**: method names and variable names are written in camelCase, that means starting with lowercase and no underscores. `file_` should be `file`, `n_localizaciones` should be `nLocalizaciones` and `LeerMatrizArchivo` should be `leerMatrizArchivo`. – MC Emperor Nov 22 '18 at 16:27
  • Thanks for your advice! Those are bad habits from my VB.NET experience... – daniel lozano Nov 23 '18 at 08:32

1 Answers1

0

Change this line:

double enlace = Double.parseDouble(split[2]);

to:

double enlace = Double.parseDouble(split[2].replace(".", ","));

The problem is the dot . you must change it to , because of your pc's local settings.

forpas
  • 160,666
  • 10
  • 38
  • 76
  • Thanks for your help! I tried but I am having the same issue. I some forum I've seen how people convert string numbers to double using the dot. Maybe could be other explanation for my "Source not found error"? I've tried everything from this web https://www.journaldev.com/18392/java-convert-string-to-double#java-convert-string-to-double, but nothing.. Also I've checked the path of the file, and the program is taking the file correctly – daniel lozano Nov 23 '18 at 08:36
  • @daniellozano which ide are you using? – forpas Nov 23 '18 at 08:58
  • @daniellozano if you are using Eclipse, see these: https://blogs.sap.com/2016/08/11/how-to-resolve-source-not-found-when-debugging-in-eclipse/ and https://stackoverflow.com/questions/14914187/why-does-the-eclipse-debugger-give-the-error-source-not-found-seemingly-withou – forpas Nov 23 '18 at 09:15
  • Thanks for the links! Following the links, I've checked both and I realized that the problem is about the Debugger, the code has not any problems. When I run the code, it runs correctly. – daniel lozano Nov 23 '18 at 11:54
  • Now that I've installed the jdk needed for the debug, and after following the steps from the links, I am having the same issue. I tried to add in Window-Preferences-Java-Installed JREs the new jdk installed, Apply & Close and restart the IDE, and same issue. Also I tried to EDIT the selected installed jre, select all JRE system libraries - Source Attachment and set the source.zip folder but same issue. More suggestions? Thanks!! – daniel lozano Nov 23 '18 at 12:00
  • If you fixed it, it's fine. – forpas Nov 23 '18 at 12:00
  • Because of your links, now I can understand where is the problem.. But I am not able to fix my problem with the debugger =( – daniel lozano Nov 23 '18 at 12:01