Error al convertir String a double
Publicado por Mario (2 intervenciones) el 27/02/2018 06:29:11
Hola a todos, el día de hoy me surgió un problema al que no le he encontrado solución y estaré muy agradecido si logran ayudarme.
El detalle es que quiero guardar una valor double que transformo de un String en una matriz, pero mi sorpresa es que al convertirlo me tira un error que dice:
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at Algoritmo_de_warshall.Warshall.main(Warshall.java:40)
Y no he podido solucionarlo.
Mi código es el siguiente, espero puedan ayudarme:
El detalle es que quiero guardar una valor double que transformo de un String en una matriz, pero mi sorpresa es que al convertirlo me tira un error que dice:
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at Algoritmo_de_warshall.Warshall.main(Warshall.java:40)
Y no he podido solucionarlo.
Mi código es el siguiente, espero puedan ayudarme:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.Scanner;
import java.lang.*;
public class Warshall {
public void imprimir (double [][]Matriz) {
//Método para imprimir matrices
for (int i = 0; i< Matriz.length; i++) {
for(int j = 0; j<Matriz[i].length; j++) {
if (Matriz[i][j]== Double.POSITIVE_INFINITY)
System.out.print(Matriz[i][j]+"\t");
else
System.out.print(Matriz[i][j]+"\t\t");
}
System.out.println();
}
}
public static void main(String[] args) {
//Declaracion de variables primitivas y de referencia
double [][]Matriz; int Vertices = 0; String Numero = ""; double Comparar = 0.0, Comparar2 =0.0;
Warshall Impresora = new Warshall ();
Scanner Entrada = new Scanner (System.in);
//Dato de entrada para asignarle memoria a la matriz
System.out.print("Ingresa el número de vértices que tiene el grafo: ");
Vertices = Entrada.nextInt();
//Asignamos memoria a la matriz
Matriz = new double [Vertices][Vertices];
//Datos de entrada para llenar matriz de k=0
System.out.println("Ingresa la matriz para k = 0\nEscribe INF si el valor es infinito");
for (int i = 0;i<Vertices; i++) {
for (int j = 0; j<Vertices; j++) {
System.out.println("Ingresa el valor para la casilla "+i+","+j);
Numero = Entrada.nextLine();
if (Numero.equals("INF"))
Matriz[i][j]= Double.POSITIVE_INFINITY;
else
Matriz[i][j]=Double.parseDouble(Numero);
}
}
System.out.println("================================================================================================");
Impresora.imprimir(Matriz);
//Procesos
for (int k= 1; k< Vertices; k++) {
System.out.println("================================================================================================");
System.out.println("Matriz k = "+k);
for(int i= 0; i<Vertices; i++) {
for(int j = 0;j< Vertices; j++) {
Comparar= Comparar2 = 0;
Comparar = Matriz[i][j];
Comparar2 = Matriz[i][k-1]+ Matriz[k-1][j];
if(Comparar <Comparar2)
Matriz[i][j] = Comparar;
else
Matriz [i][j]= Comparar2;
}
}
Impresora.imprimir(Matriz);
System.out.println("================================================================================================");
}
}
}
Valora esta pregunta
0