Source not found error when split and covert to double a value read from text file
up vote
0
down vote
favorite
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.
java
add a comment |
up vote
0
down vote
favorite
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.
java
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 befile
,n_localizaciones
should benLocalizaciones
andLeerMatrizArchivo
should beleerMatrizArchivo
.
– MC Emperor
Nov 22 at 16:27
Thanks for your advice! Those are bad habits from my VB.NET experience...
– daniel lozano
Nov 23 at 8:32
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
java
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.
java
java
asked Nov 22 at 16:10
daniel lozano
67111
67111
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 befile
,n_localizaciones
should benLocalizaciones
andLeerMatrizArchivo
should beleerMatrizArchivo
.
– MC Emperor
Nov 22 at 16:27
Thanks for your advice! Those are bad habits from my VB.NET experience...
– daniel lozano
Nov 23 at 8:32
add a comment |
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 befile
,n_localizaciones
should benLocalizaciones
andLeerMatrizArchivo
should beleerMatrizArchivo
.
– MC Emperor
Nov 22 at 16:27
Thanks for your advice! Those are bad habits from my VB.NET experience...
– daniel lozano
Nov 23 at 8:32
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 at 16:27
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 at 16:27
Thanks for your advice! Those are bad habits from my VB.NET experience...
– daniel lozano
Nov 23 at 8:32
Thanks for your advice! Those are bad habits from my VB.NET experience...
– daniel lozano
Nov 23 at 8:32
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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.
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 journaldev.com/18392/…, but nothing.. Also I've checked the path of the file, and the program is taking the file correctly
– daniel lozano
Nov 23 at 8:36
@daniellozano which ide are you using?
– forpas
Nov 23 at 8:58
@daniellozano if you are using Eclipse, see these: blogs.sap.com/2016/08/11/… and stackoverflow.com/questions/14914187/…
– forpas
Nov 23 at 9: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 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 at 12:00
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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.
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 journaldev.com/18392/…, but nothing.. Also I've checked the path of the file, and the program is taking the file correctly
– daniel lozano
Nov 23 at 8:36
@daniellozano which ide are you using?
– forpas
Nov 23 at 8:58
@daniellozano if you are using Eclipse, see these: blogs.sap.com/2016/08/11/… and stackoverflow.com/questions/14914187/…
– forpas
Nov 23 at 9: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 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 at 12:00
|
show 2 more comments
up vote
0
down vote
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.
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 journaldev.com/18392/…, but nothing.. Also I've checked the path of the file, and the program is taking the file correctly
– daniel lozano
Nov 23 at 8:36
@daniellozano which ide are you using?
– forpas
Nov 23 at 8:58
@daniellozano if you are using Eclipse, see these: blogs.sap.com/2016/08/11/… and stackoverflow.com/questions/14914187/…
– forpas
Nov 23 at 9: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 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 at 12:00
|
show 2 more comments
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Nov 22 at 16:22
forpas
6,1581218
6,1581218
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 journaldev.com/18392/…, but nothing.. Also I've checked the path of the file, and the program is taking the file correctly
– daniel lozano
Nov 23 at 8:36
@daniellozano which ide are you using?
– forpas
Nov 23 at 8:58
@daniellozano if you are using Eclipse, see these: blogs.sap.com/2016/08/11/… and stackoverflow.com/questions/14914187/…
– forpas
Nov 23 at 9: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 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 at 12:00
|
show 2 more comments
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 journaldev.com/18392/…, but nothing.. Also I've checked the path of the file, and the program is taking the file correctly
– daniel lozano
Nov 23 at 8:36
@daniellozano which ide are you using?
– forpas
Nov 23 at 8:58
@daniellozano if you are using Eclipse, see these: blogs.sap.com/2016/08/11/… and stackoverflow.com/questions/14914187/…
– forpas
Nov 23 at 9: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 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 at 12:00
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 journaldev.com/18392/…, but nothing.. Also I've checked the path of the file, and the program is taking the file correctly
– daniel lozano
Nov 23 at 8:36
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 journaldev.com/18392/…, but nothing.. Also I've checked the path of the file, and the program is taking the file correctly
– daniel lozano
Nov 23 at 8:36
@daniellozano which ide are you using?
– forpas
Nov 23 at 8:58
@daniellozano which ide are you using?
– forpas
Nov 23 at 8:58
@daniellozano if you are using Eclipse, see these: blogs.sap.com/2016/08/11/… and stackoverflow.com/questions/14914187/…
– forpas
Nov 23 at 9:15
@daniellozano if you are using Eclipse, see these: blogs.sap.com/2016/08/11/… and stackoverflow.com/questions/14914187/…
– forpas
Nov 23 at 9: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 at 11:54
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 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 at 12:00
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 at 12:00
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434761%2fsource-not-found-error-when-split-and-covert-to-double-a-value-read-from-text-fi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 befile
,n_localizaciones
should benLocalizaciones
andLeerMatrizArchivo
should beleerMatrizArchivo
.– MC Emperor
Nov 22 at 16:27
Thanks for your advice! Those are bad habits from my VB.NET experience...
– daniel lozano
Nov 23 at 8:32