Ejercicios resueltos de ficheros clase escanner
Publicado por burlon (14 intervenciones) el 08/06/2015 22:10:56
Realiza un programa en JAVA que lea con la clase Scanner un archivo creado en el bloc de notas llamado masnumeros.txt que contiene varios números separados por ; (punto y coma).
• El programa te dará la suma de todos los números del fichero.
Realiza un programa en JAVA que lea con la clase Scanner dos archivos
creados en el bloc de notas llamados pares.txt e impares.txt, que contiene
varios números separados por guiones. Luego, el programa, te creará un
fichero llamado resultados.txt que contendrá la suma de los números de
los ficheros pares.txt e impares.txt separados por guiones.
Utiliza expresiones regulares para comprobar que la fecha de nacimiento que te introduzca un usuario cumple con el patrón dd/mm/aaaa.
Además, un día tomará valores entre 0 y 31, el mes entre 0 y 12 y el año
entre 1800 y 2016.
• El programa te dará la suma de todos los números del fichero.
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
public class Ejercicio22tema08 {
public static void mostrarFichero(int[] numerosInt) {
Scanner entrada = null;
try {
File fichero = new File("masnumeros.txt");
entrada = new Scanner(fichero);
leerFichero(entrada, numerosInt);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
if (entrada != null) {
entrada.close();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public static void leerFichero(Scanner entrada, int[] numerosInt) throws Exception {
String numero;
int suma = 0;
while (entrada.hasNext()) {
numero = entrada.nextLine();
Scanner lineaTroceada = new Scanner(numero).useDelimiter("\\s*;\\s*");
for (int i = 0; i < numerosInt.length; i++) {
numerosInt[i] = Integer.parseInt(lineaTroceada.next());
System.out.println(numerosInt[i]);
suma = suma + numerosInt[i];
}
System.out.println("La suma de los numeros es de: " + suma);
}
}
public static void main(String[] args) {
int[] numerosInt = new int[7];
mostrarFichero(numerosInt);
}
}
Realiza un programa en JAVA que lea con la clase Scanner dos archivos
creados en el bloc de notas llamados pares.txt e impares.txt, que contiene
varios números separados por guiones. Luego, el programa, te creará un
fichero llamado resultados.txt que contendrá la suma de los números de
los ficheros pares.txt e impares.txt separados por guiones.
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
public class Ejercicio23tema08 {
public static void mostrarFicheroPares(String[] trozosPares) {
Scanner entrada = null;
try {
File fichero = new File("pares.txt");
entrada = new Scanner(fichero);
leerFicheroPares(entrada, trozosPares);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
if (entrada != null) {
entrada.close();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public static void leerFicheroPares(Scanner entrada, String[] trozosPares) throws Exception {
String linea;
while (entrada.hasNextLine()) {
linea = entrada.nextLine();
trozosPares = linea.split("[-]");
for (int i = 0; i < trozosPares.length; i++) {
System.out.println(trozosPares[i]);
}
}
}
public static void mostrarFicheroImpare(String[] trozosImp) {
Scanner entrada = null;
try {
File fichero = new File("impares.txt");
entrada = new Scanner(fichero);
leerFicheroImpares(entrada, trozosImp);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
if (entrada != null) {
entrada.close();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public static void leerFicheroImpares(Scanner entrada, String[] trozosImp) throws Exception {
String linea;
while (entrada.hasNextLine()) {
linea = entrada.nextLine();
trozosImp = linea.split("[-]");
for (int i = 0; i < trozosImp.length; i++) {
System.out.println(trozosImp[i]);
}
}
}
public static void llamarFichero(String[] trozosImp, String[] trozosPares) {
FileWriter fw = null;
try {
fw = new FileWriter("resultado.txt");
PrintWriter pw = new PrintWriter(fw);
escribirFichero(pw, trozosPares, trozosImp);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
if (fw != null) {
fw.close();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public static void escribirFichero(PrintWriter pw, String[] trozosPares, String[] trozosImp) {
int suma;
for (int i = 0; i < trozosImp.length; i++) {
System.out.println(trozosImp[i]);
}
int[] numerosImp = new int[trozosImp.length];
int[] numerosPar = new int[trozosPares.length];
for (int i = 0; i < numerosImp.length; i++) {
suma = numerosImp[i] + numerosPar[i];
pw.println(suma);
System.out.println("Impares: " + numerosImp[i]);
System.out.println("Pares: " + numerosPar[i]);
System.out.println("Resultado: " + suma);
}
}
public static void main(String[] args) {
String[] trozosPares = new String[5];
String[] trozosImp = new String[5];
mostrarFicheroPares(trozosPares);
mostrarFicheroImpare(trozosImp);
llamarFichero(trozosImp, trozosPares);
}
}
Utiliza expresiones regulares para comprobar que la fecha de nacimiento que te introduzca un usuario cumple con el patrón dd/mm/aaaa.
Además, un día tomará valores entre 0 y 31, el mes entre 0 y 12 y el año
entre 1800 y 2016.
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
public class Ejercicio24tema08 {
public static String solicitarFecha() {
Scanner teclado = new Scanner(System.in);
String fecha;
System.out.println("Introduce tu fecha de nacimiento");
fecha = teclado.nextLine();
return fecha;
}
public static void comprobarFecha(String fecha) {
System.out.println(fecha);
String[] trozos = fecha.split("[/]");
int[] numeros = new int[trozos.length];
for (int i = 0; i < trozos.length; i++) {
numeros[i] = Integer.parseInt(trozos[i]);
}
if (fecha.matches("[0-3][0-9]\\/[0-1][0-9]\\/[0-9][0-9][0-9][0-9]")) {
if (numeros[0] <= 31) {
if (numeros[1] <= 12) {
if ((numeros[2] >= 1800) && (numeros[2] <= 2016)) {
System.out.println("Fecha Correcta");
} else {
System.out.println("Fecha incorecta");
}
} else {
System.out.println("Fecha incorecta");
}
} else {
System.out.println("Fecha incorecta");
}
} else {
System.out.println("Fecha incorecta");
}
}
public static void main(String[] args) {
String fecha = new String(solicitarFecha());
comprobarFecha(fecha);
}
}
Valora esta pregunta


1