I have this TXT:
1,2,4,8,7
45,18,27,35,44
17,45,2,8,6
And I want to read this array to a Java array. Here is the code:
public static void main(String[] args){
double[][] pesos = null;
String valorFinal = "";
String caracterAAdicionar = null;
Double valorAAdicionar = null;
try{
FileReader arq = new FileReader("Entrada.txt");
BufferedReader lerArq = new BufferedReader(arq);
pesos = new double[3][5];
/* Lendo matriz */
for(int i=0; i < 3; i++){
for(int j = 0; j < 5; j++){
if(j==4) //se estou no último valor da linha...
pesos[i][j] =Double.parseDouble(lerArq.read()); //...apenas leio esse valor
else{
System.out.println(lerArq.read());
caracterAAdicionar = String.valueOf(lerArq.read());
while (!caracterAAdicionar.equals(",")){
valorFinal = valorFinal + caracterAAdicionar;
lerArq.read();
}
}
pesos[i][j] = Double.parseDouble(valorFinal);
}
}
arq.close();
}
catch(FileNotFoundException f){
f.printStackTrace();
}
catch(InputMismatchException f){
/*System.out.print("Erro ocorreu em: " + i + " " + j);*/
f.printStackTrace();
}
catch(IOException f){
/*System.out.print("Erro ocorreu em: " + i + " " + j);*/
f.printStackTrace();
}
}
Problem: No While ( while (!caracterAAdicionar.equals(",")){
), the program goes into infinite loop. Then, to debug, I give a print in lerArq.read(
) before entering this loop for the 1st time (as it is already in the code). And the result?
49
Yes, 49.E, in the loop, the program will print "49" forever.
But where the hell did he get that number from?