Java - AYUDEN POR FAVOR - ARCHIVOS DE ACCESO ALEATORIO

 
Vista:
sin imagen de perfil
Val: 5
Ha aumentado su posición en 6 puestos en Java (en relación al último mes)
Gráfica de Java

AYUDEN POR FAVOR - ARCHIVOS DE ACCESO ALEATORIO

Publicado por Jafet (1 intervención) el 30/04/2021 15:39:30
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
package tutorial2;
 
import java.util.Scanner;
import java.io.*;
 
/*El departamento de personal de una universidad tiene registros del nombre,
sexo y edad de cada uno de los profesores inscritos.
Escriba un programa que calcule e imprima los siguientes datos (primero debe llenar el Archivo de información):
Edad promedio del grupo de profesores
i. Nombre del profesor más joven del grupo
ii. Nombre del profesor de más edad
iii. Número de profesores con edad mayor al promedio
iv. Número de profesores con edad menor al promedio
Nota: cada una de las opciones del programa, debe ser manejada por medio de un Menú.
Además de la opción para ingresar por primera vez los datos de los profesores.
ya tengo los metodos de hallar menor y mayor leyendo la variable entera dentro del archivo
pero no se como mostrar el String correspondiente a la edad mayor o menor que se encontro*/
 
public class Tutorial2 {
 
    static RandomAccessFile archivo;
 
    static void escribir(String nombre, char sexo, int edad) throws IOException {
        archivo.seek(archivo.length()); //el puntero se posiciona al final
        archivo.writeUTF(nombre);
        archivo.writeChar(sexo);
        archivo.writeInt(edad);
    }
 
 
    static void imprimir() throws IOException {
        archivo.seek(0);
        while(archivo.getFilePointer() < archivo.length()) {
            System.out.println(archivo.readUTF());
            System.out.println(archivo.readChar());
            System.out.println(archivo.readInt());
        }
    }
 
    static void menor() throws IOException {
        int edad2;
        int menor=1000;
        int i;
        archivo.seek(0);
        while(archivo.getFilePointer() < archivo.length()) {
            archivo.readUTF();
            archivo.readChar();
            edad2 = archivo.readInt();
            for(i = 0; i < archivo.length() ;i++ ){
                if(edad2<menor)
                {
                    menor=edad2;
                }
            }
        }
        System.out.println(menor);
 
    }
 
    static void mayor() throws IOException {
        int edad1;
        int mayor=0;
        int i;
        archivo.seek(0);
        while(archivo.getFilePointer() < archivo.length()) {
            archivo.readUTF();
            archivo.readChar();
            edad1 = archivo.readInt();
            for(i = 0; i < archivo.length() ;i++ ){
                if(edad1>mayor)
                {
                    mayor=edad1;
                }
            }
        }
 
 
        System.out.println(mayor);
 
    }
 
 
 
    public static void main(String[] args) {
        try {
            //r
            //rw
            archivo = new RandomAccessFile("Personas.dat", "rw");
        } catch(IOException e) {
 
        }
        Scanner scanner = new Scanner(System.in);
        int op;
        do {
            System.out.println("1. Registrar Profesor ");
            System.out.println("2. Mostrar Profesores ");
            System.out.println("3. Mostrar profesor mas joven del grupo ");
            System.out.println("4. Mostrar profesor de mas edad ");
            System.out.println("5. SALIR ");
            System.out.print("Elija una opcion (1-5): ");
            op = scanner.nextInt();
            System.out.println();
            switch(op) {
                case 1: {
                    String nombre;
                    int edad;
                    char sexo;
                    System.out.print("Ingrese nombre: ");
                    nombre = scanner.next();
                    System.out.print("Ingrese sexo: ");
                    sexo = scanner.next().charAt(0);
                    System.out.print("Ingrese edad: ");
                    edad = scanner.nextInt();
                    try {
                        escribir(nombre, sexo, edad);
                    } catch(IOException e) {
 
                    }
                    System.out.println("Se ha registrado correctamente!");
                    break;
                }
                case 2: {
                    try {
                        imprimir();
                    } catch(IOException e) {
 
                    }
                    break;
 
                }
                case 3: {
                    try {
                        menor();
                    } catch(IOException e) {
 
                    }
                    break;
                }
                case 4: {
                    try {
                        mayor();
                    } catch(IOException e) {
 
                    }
                    break;
                }
                default: {
                    if(op != 5)
                        System.out.println("Opcion no valida");
                }
            }
            System.out.println();
        } while(op != 5);
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
-1
Responder