Lectura de dos archivos desde un txt y mostrarlos en consola.
Publicado por José Luis (1 intervención) el 23/03/2021 04:57:23
Hola, he tenido un problema en java, lo que ocurre es que tengo dos archivos (matrizA.txt, matrizB.txt) de texto, ambos con la misma información:
2
2
2.3+3.4 3.4+2.9
2.0+3.0 3.4+2.3
Los dos primeros números pertenecen al numero de filas y columnas respectivamente, mientras la matriz se compone de flotantes donde el numero antes de "+" es real y el que va después es un numero imaginario.
-->Adjunto mi código a continuación:
La consola si reconoce los valores de filas y columnas pero al llegar a la matriz, me manda el mensaje que no los reconoce, como puedo hacer que se muestre dichas matrices en consola?
2
2
2.3+3.4 3.4+2.9
2.0+3.0 3.4+2.3
Los dos primeros números pertenecen al numero de filas y columnas respectivamente, mientras la matriz se compone de flotantes donde el numero antes de "+" es real y el que va después es un numero imaginario.
-->Adjunto mi código a continuación:
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import java.util.*;
import java.util.Scanner;
import java.util.regex.Pattern;
//Para poder leer el archivo
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
class COMPLEJO
{
//Declaracion de atributos
private float real;
private float imaginario;
//Metodo constructor
COMPLEJO() //Constructor por default.
{
real = 0;
imaginario = 0;
System.out.println("Se ha ejecutado el contrsuctor por defecto");
}
//Constructor con parametros: Sirve para el metodo de comlejo multipliacion de un solo parametro.
COMPLEJO(float r, float i)
{
real = r;
imaginario = i;
}
//Para leer cada uno de los txt.
public void CapturaComplejoA() throws Exception
{
String archivo = "MatricesA.txt";
Scanner Lectura;
Lectura = new Scanner(new File(archivo));
System.out.println(" ");
System.out.print("real = \t\t");
real = Lectura.nextFloat();
System.out.print("imaginario = \t");
imaginario = Lectura.nextFloat();
}
public void CapturaComplejoB() throws Exception
{
String archivo = "MatricesB.txt";
Scanner Lectura;
Lectura = new Scanner(new File(archivo));
System.out.println(" ");
System.out.print("real = \t\t");
real = Lectura.nextFloat();
System.out.print("imaginario = \t");
imaginario = Lectura.nextFloat();
}
}
class MATRIZCOMPLEJA
{
private COMPLEJO [][]Mx; //Variable de referencia.
private int M; //Filas
private int N; //Columnas
private int[][] ResultadoTXT = new int[M][N];
MATRIZCOMPLEJA() //Metodo constructor.
{
M = 0;
N = 0;
Mx = null;
}
//Constructor con parametros
MATRIZCOMPLEJA(int Filas, int Columnas)
{
int i, j;
M = Filas;
N = Columnas;
Mx = new COMPLEJO[M][N];
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
Mx[i][j] = new COMPLEJO();
}
}
}
//Para la lectura del primer txt
public void LeerMatrizComplejaA() throws Exception//throws FileNotFoundExceptionthrows Exception
{
String archivo = "MatricesA.txt";
Scanner Lectura;
Lectura = new Scanner(new File(archivo));
int M,N, i, j;
FileReader fr = null;
BufferedReader br;
MATRIZCOMPLEJA MatrizA[][];
System.out.println("Numero de filas: ");
M = Lectura.nextInt();
System.out.println(M);
System.out.println("Numero de columnas: ");
N = Lectura.nextInt();
System.out.println(N);
MatrizA = new MATRIZCOMPLEJA[M][N];
try
{
if(Mx != null)
{
fr = new FileReader(archivo);
br = new BufferedReader(fr);
String linea; //será utilizada para leer el contenido del archivo linea a linea
while((linea = br.readLine()) != null)
{
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
Mx[i][j].CapturaComplejoA();
System.out.println(MatrizA[i][j] + " \t");
}
}
}
}
else
{
System.out.println("Hay un problema al leer el archivo.");
}
}
catch (FileNotFoundException e)
{
System.out.println("No se encuentra archivo.");
e.printStackTrace();
}
catch (NumberFormatException e)
{
System.out.println("No se pudo convertir a entero.");
e.printStackTrace();
}
catch (IOException e)
{
System.out.println("Error accediendo al archivo.");
e.printStackTrace();
}
}
//Para la lectura del segundo txt
public void LeerMatrizComplejaB() throws Exception
{
String archivo = "MatricesB.txt";
Scanner Lectura;
Lectura = new Scanner(new File(archivo));
int M,N, i, j;
FileReader fr = null;
BufferedReader br;
MATRIZCOMPLEJA MatrizB[][];
System.out.println("Numero de filas: ");
M = Lectura.nextInt();
System.out.println(M);
System.out.println("Numero de columnas: ");
N = Lectura.nextInt();
System.out.println(N);
MatrizB = new MATRIZCOMPLEJA[M][N];
try
{
if(Mx != null)
{
fr = new FileReader(archivo);
br = new BufferedReader(fr);
String linea; //será utilizada para leer el contenido del archivo linea a linea
while((linea = br.readLine()) != null)
{
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
Mx[i][j].CapturaComplejoB();
System.out.println(MatrizB[i][j] + " \t");
}
}
}
}
else
{
System.out.println("Hay un problema al leer el archivo.");
}
}
catch (FileNotFoundException e)
{
System.out.println("No se encuentra archivo.");
e.printStackTrace();
}
catch (NumberFormatException e)
{
System.out.println("No se pudo convertir a entero.");
e.printStackTrace();
}
catch (IOException e)
{
System.out.println("Error accediendo al archivo.");
e.printStackTrace();
}
}
}
class practica4
{
public static void main(String arg[]) throws Exception
{
//Se crea la matriz compleja
MATRIZCOMPLEJA A; //Variable de referencia
A = new MATRIZCOMPLEJA(); //Se asigna una nueva variable
MATRIZCOMPLEJA B; //Variable de referencia
B = new MATRIZCOMPLEJA();
MATRIZCOMPLEJA C; //Variable de referencia
C = new MATRIZCOMPLEJA();
MATRIZCOMPLEJA P; //Variable de referencia
P = new MATRIZCOMPLEJA();
A.LeerMatrizComplejaA();
B.LeerMatrizComplejaB();
}
}
La consola si reconoce los valores de filas y columnas pero al llegar a la matriz, me manda el mensaje que no los reconoce, como puedo hacer que se muestre dichas matrices en consola?
Valora esta pregunta


0