Java - Elabora un programa que capture en un Array los datos

 
Vista:

Elabora un programa que capture en un Array los datos

Publicado por angel (4 intervenciones) el 03/05/2021 15:15:12
Elabora un programa que capture en un Array los datos nombre, apellidos, cedula y
edad de tres personas. Luego de capturados los datos, debe imprimir la información
de acuerdo a la edad, de mayor a menor edad
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Billy Joel
Val: 2.665
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Elabora un programa que capture en un Array los datos

Publicado por Billy Joel (874 intervenciones) el 03/05/2021 16:57:44
Yo lo resuelvo así

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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
//import java.util.Comparator;
//import java.util.stream.Collectors;
 
/**
 *
 * @author billy.johnson
 */
public class Datos {
 
    String nombre;
    String apellidos;
    String cedula;
    int edad;
 
    public Datos(String nombre, String apellidos, String cedula, int edad) {
        this.nombre = nombre;
        this.apellidos = apellidos;
        this.cedula = cedula;
        this.edad = edad;
    }
 
    public int getEdad() {
        return edad;
    }
 
    @Override
    public String toString() {
        return "Nombre: " + nombre
                + "\nApellidos: " + apellidos
                + "\nCedula: " + cedula
                + "\nEdad: " + edad
                + "\n------------------------------------------\n";
    }
 
    public static void main(String[] args) {
        List<Datos> array = new ArrayList();
        for (int i = 1; i <= 3; i++) {
            System.out.println("Capturando datos #" + i);
            array.add(leerDatos());
            System.out.println();
        }
 
//        /**
//         * Test Data
//         */
//        List<Datos> array = new ArrayList();
//        array.add(new Datos("j", "s", "9999", 34));
//        array.add(new Datos("b", "j", "8888", 35));
//        array.add(new Datos("a", "m", "1111", 23));
        /////////////////////////////JAVA 8/////////////////////////////////////
//        List<Datos> ordenado = array
//                .stream()
//                .sorted(Comparator.comparingInt(Datos::getEdad).reversed())
//                .collect(Collectors.toList());
//        ordenado.forEach(System.out::println);
        /////////////////////////////JAVA 8/////////////////////////////////////
        ///////////////////ORDENAMIENTO POR BURBUJA/////////////////////////////
        Datos[] array2 = new Datos[array.size()];
        array.toArray(array2);
        for (int i = array2.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (j + 1 <= i && array2[j].getEdad() < array2[j + 1].getEdad()) {
                    Datos aux = array2[j];
                    array2[j] = array2[j + 1];
                    array2[j + 1] = aux;
                }
            }
        }
 
        for (Datos d : array2) {
            System.out.println(d);
        }
        ///////////////////ORDENAMIENTO POR BURBUJA/////////////////////////////
    }
 
    public static Datos leerDatos() {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            System.out.print("Introduce el nombre: ");
            String nombre = br.readLine();
            System.out.print("Introduce los apellidos: ");
            String apellidos = br.readLine();
            System.out.print("Introduce la cedula: ");
            String cedula = br.readLine();
            System.out.print("Introduce la edad: ");
            String edad = br.readLine();
            return new Datos(nombre, apellidos, cedula, Integer.parseInt(edad));
        } catch (IOException ex) {
            ex.printStackTrace(System.out);
            System.out.println("Hubo un error de lectura, favor intentar de nuevo...");
            return leerDatos();
        }
    }
}

Para que entiendas un poco lo que se hace en JAVA 8
https://mkyong.com/java8/java-8-how-to-sort-list-with-stream-sorted/

Ordenamiento de burbuja
https://www.lawebdelprogramador.com/foros/Java/1765156-Tres-mejores-tiempos.html#i1765502
Si hubiese que cambiar el ordenamiento de menor a mayor solo hay que cambiar el signo de menor que a mayor que... Pues que quede así:
1
if (j + 1 <= i && array2[j].getEdad() > array2[j + 1].getEdad()) {

Saludos,
Billy Joel
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar