Java - Necesito ayuda con mi codigo

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

Necesito ayuda con mi codigo

Publicado por Nacho (4 intervenciones) el 24/06/2019 04:28:01
El problema es el siguiente, en el caso 3 y 4 el ciclo for no recorre el array, por lo tanto si el paciente esta ingresado en la posicion 1 (contando desde el 0) el ciclo nunca llega a esa posicion, precisaria saber cual es mi error, desde ya muchas gracias.

Aqui el codigo:

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
package pacientes;
import java.util.Scanner;
 
public class Pacientes {
    public static void main(String[] args) {
 
    String[] nombre = new String[100];
    int[] edad = new int [100];
    Scanner sc = new Scanner(System.in);
 
 
    System.out.println("******** GESTION DE PACIENTES ********"); System.out.println();
    System.out.println("1. INGRESAR NUEVO PACIENTE");
    System.out.println("2. BUSCAR PACIENTE");
    System.out.println("3. ELIMINAR PACIENTE");
    System.out.println("4. MODIFICAR PACIENTE");
    System.out.println("5. VER TODOS LOS PACIENTES");
    System.out.println("6. SALIR"); System.out.println();
 
    int opcion;
    int x = 0;
    int i;
    boolean encontrado = false;
    String espacio;
 
    do {
    System.out.print("ELIJA UNA OPCION: ");
    opcion = sc.nextInt();
 
    switch (opcion) {
        case 1:
            System.out.print("INGRESE EL NOMBRE DEL PACIENTE: "); System.out.println();
            espacio = sc.nextLine();
            nombre[x] = sc.nextLine();
            System.out.print("INGRESE EDAD DEL PACIENTE: "); System.out.println();
            edad[x] = sc.nextInt();
            x++;
        break;
 
        case 2:
            String ingreso;
            System.out.print("INGRESE NOMBRE DEL PACIENTE: "); System.out.println();
            espacio = sc.nextLine();
            ingreso = sc.nextLine();
 
            for (i = 0; i < nombre.length; i++) {
                if (ingreso.equals(nombre[i])) {
                    System.out.println("EL PACIENTE INGRESADO ES CORRECTO");
                    System.out.println("EDAD DEL PACIENTE: " + edad[i] + " AÑOS.");
                    encontrado = true;
                }
            }
 
            if (!encontrado) {
                    System.out.println("EL PACIENTE NO FUE INGRESADO");
                }
        break;
 
        case 3:
            String ingreso2;
            System.out.print("INGRESE EL NOMBRE DEL PACIENTE A ELIMINAR: ");
            espacio = sc.nextLine();
            ingreso2 = sc.nextLine();
 
 
            for (i = 0; i < nombre.length; i++) {
                if (ingreso2.equals(nombre[i])) {
                    nombre[i] = null;
                    edad[i] = 0;
                    System.out.println("EL PACIENTE FUE ELIMINADO");
                    break;
                }
 
                else {
                    System.out.println("EL PACIENTE NO FUE ENCONTRADO");
                    break;
                }
            }
        break;
 
        case 4:
            System.out.print("INGRESE NOMBRE COMPLETO DEL PACIENTE A MODIFICAR: ");
            espacio = sc.nextLine();
            String mod_nombre = sc.nextLine();
 
            for (i = 0; i < nombre.length; i++) {
                if (mod_nombre.equals(nombre[i])) {
                    System.out.println("INGRESE EL NUEVO NOMBRE DEL PACIENTE: ");
                    nombre[i] = sc.nextLine();
                    System.out.println("INGRESE NUEVA EDAD DEL PACIENTE: ");
                    edad[i] = sc.nextInt();
 
                    System.out.println("DATOS ACTUALIZADOS DEL PACIENTE");
                    System.out.println("NOMBRE: " + nombre[i] + " EDAD: " + edad[i]);
                    break;
                }
 
                else {
                    System.out.println("EL PACIENTE NO FUE ENCONTRADO");
                    break;
                }
            }
        break;
 
        case 5:
            for (i = 0; i < nombre.length; i++) {
                if(nombre[i]!= null) {
                    System.out.println("PACIENTE: " + nombre[i] + " EDAD: " + edad[i]);
                }
            }
        break;
 
 
        }
    } while (opcion != 6);
 
    if (opcion == 6) {
        System.out.println("USTED SALIO DEL SISTEMA.");
    }
 
    }
}
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 Sebastian
Val: 282
Ha disminuido 1 puesto en Java (en relación al último mes)
Gráfica de Java

Necesito ayuda con mi codigo

Publicado por Sebastian (91 intervenciones) el 24/06/2019 09:03:56
Es por que estas validando la respuesta positiva y negativa en cada posición, por lo tanto solo recorre la primera posición y si no es igual el nombre, te dice que no es encontrado, tienes que solo validar que lo encuentre mientras recorres todo el vector y si no lo encontro, mostrar el mensaje.

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
case 3:
            String ingreso2;
            System.out.print("INGRESE EL NOMBRE DEL PACIENTE A ELIMINAR: ");
            espacio = sc.nextLine();
            ingreso2 = sc.nextLine();
 
 
            for (i = 0; i < nombre.length; i++) {
                if (ingreso2.equals(nombre[i])) {
                    nombre[i] = null;
                    edad[i] = 0;
                    System.out.println("EL PACIENTE FUE ELIMINADO");
                    encontrado = true;
                    break;
                }
            }
 
            if (!encontrado) {
                    System.out.println("EL PACIENTE NO FUE ENCONTRADO");
                }
 
        break;
 
        case 4:
            System.out.print("INGRESE NOMBRE COMPLETO DEL PACIENTE A MODIFICAR: ");
            espacio = sc.nextLine();
            String mod_nombre = sc.nextLine();
 
            for (i = 0; i < nombre.length; i++) {
                if (mod_nombre.equals(nombre[i])) {
                    System.out.println("INGRESE EL NUEVO NOMBRE DEL PACIENTE: ");
                    nombre[i] = sc.nextLine();
                    System.out.println("INGRESE NUEVA EDAD DEL PACIENTE: ");
                    edad[i] = sc.nextInt();
 
                    System.out.println("DATOS ACTUALIZADOS DEL PACIENTE");
                    System.out.println("NOMBRE: " + nombre[i] + " EDAD: " + edad[i]);
                    encontrado = true;
                    break;
                }
            }
 
            if (!encontrado) {
                    System.out.println("EL PACIENTE NO FUE
                             ENCONTRADO");
                }
 
        break;
 
        case 5:
            for (i = 0; i < nombre.length; i++) {
                if(nombre[i]!= null) {
                    System.out.println("PACIENTE: " +
                              nombre[i] + " EDAD: " + edad[i]);
                }
            }
        break;
 
 
        }
 
        encontrado = false;
 
    } while (opcion != 6);
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil
Val: 7
Ha aumentado su posición en 5 puestos en Java (en relación al último mes)
Gráfica de Java

Necesito ayuda con mi codigo

Publicado por Nacho (4 intervenciones) el 25/06/2019 04:47:32
Muchas gracias!!!!
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
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

Necesito ayuda con mi codigo

Publicado por Billy Joel (876 intervenciones) el 24/06/2019 20:20:19
Tienes un if-else dentro del for
1
2
3
4
5
6
7
8
9
10
11
for (i = 0; i < nombre.length; i++) {
    if (ingreso2.equals(nombre[i])) {
        nombre[i] = null;
        edad[i] = 0;
        System.out.println("EL PACIENTE FUE ELIMINADO");
        break;
    } else {
        System.out.println("EL PACIENTE NO FUE ENCONTRADO");
        break;
    }
}

Dentro de las opciones tienes el break; que rompe el bucle y es por esto que solo evalúa la primera posición del arreglo...
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
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

Necesito ayuda con mi codigo

Publicado por Billy Joel (876 intervenciones) el 24/06/2019 21:03:51
Bueno me he puesto curioso y lo he hecho de otra forma haciendo uso de Maps
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
 
public class Pacientes {
 
    public static final int OPCION_INGRESAR_NUEVO_PACIENTE = 1;
    public static final int OPCION_BUSCAR_PACIENTE = 2;
    public static final int OPCION_ELIMINAR_PACIENTE = 3;
    public static final int OPCION_MODIFICAR_PACIENTE = 4;
    public static final int OPCION_MOSTRAR_PACIENTES = 5;
    public static final int OPCION_SALIR = 6;
 
    static Map<String, Paciente> pacientes = new HashMap();
 
    /**
     * Metodo utilizado para leer
     *
     * @param message
     * @return
     */
    public static String leer(String message) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        try {
            System.out.print(message);
            s = br.readLine();
        } catch (IOException ex) {
            System.out.println("Hubo un error de lectura vuelva a intentarlo");
            s = null;
        }
        if (s == null) {
            s = leer(message);
        }
        return s;
    }
 
    /**
     * Metodo utilizado para leer enteros
     *
     * @param message
     * @return
     */
    public static Integer leerInt(String message) {
        Integer i;
        try {
            i = Integer.parseInt(leer(message));
        } catch (NumberFormatException ex) {
            System.out.println("Formato incorrecto, vuelva a intentarlo");
            i = null;
        }
        if (i == null) {
            i = leerInt(message);
        }
        return i;
    }
 
    public static void main(String[] args) {
        int opcion;
        do {
            opcion = leerInt("******** GESTION DE PACIENTES ********"
                    + "\n" + OPCION_INGRESAR_NUEVO_PACIENTE + ". INGRESAR NUEVO PACIENTE"
                    + "\n" + OPCION_BUSCAR_PACIENTE + ". BUSCAR PACIENTE"
                    + "\n" + OPCION_ELIMINAR_PACIENTE + ". ELIMINAR PACIENTE"
                    + "\n" + OPCION_MODIFICAR_PACIENTE + ". MODIFICAR PACIENTE"
                    + "\n" + OPCION_MOSTRAR_PACIENTES + ". VER TODOS LOS PACIENTES"
                    + "\n" + OPCION_SALIR + ". SALIR"
            );
            switch (opcion) {
                case OPCION_INGRESAR_NUEVO_PACIENTE:
                    ingresarPaciente();
                    break;
                case OPCION_BUSCAR_PACIENTE:
                    buscar();
                    break;
                case OPCION_ELIMINAR_PACIENTE:
                    eliminar();
                    break;
                case OPCION_MODIFICAR_PACIENTE:
                    modificar();
                    break;
                case OPCION_MOSTRAR_PACIENTES:
                    listar();
                    break;
                case OPCION_SALIR:
                    salir();
                    break;
                default:
                    System.out.println("Opción incorrecta");
                    break;
            }
        } while (opcion != OPCION_SALIR);
    }
 
    public static void ingresarPaciente() {
        String nombre = leer("INGRESE EL NOMBRE DEL PACIENTE: ");
        int edad = leerInt("INGRESE EDAD DEL PACIENTE: ");
        Paciente p = new Paciente();
        p.setNombre(nombre);
        p.setEdad(edad);
        pacientes.put(nombre, p);
        System.out.println("Paciente registrado");
    }
 
    public static void buscar() {
        String nombre = leer("INGRESE EL NOMBRE DEL PACIENTE: ");
        if (pacientes.containsKey(nombre)) {
            System.out.println("EL PACIENTE INGRESADO ES CORRECTO"
                    + "\nEDAD DEL PACIENTE: " + pacientes.get(nombre).getEdad() + " AÑOS."
            );
        } else {
            System.out.println("EL PACIENTE NO FUE INGRESADO");
        }
    }
 
    public static void eliminar() {
        String nombre = leer("INGRESE EL NOMBRE DEL PACIENTE: ");
        if (pacientes.containsKey(nombre)) {
            pacientes.remove(nombre);
            System.out.println("EL PACIENTE FUE ELIMINADO");
        } else {
            System.out.println("EL PACIENTE NO FUE ENCONTRADO");
        }
    }
 
    public static void modificar() {
        String nombre = leer("INGRESE NOMBRE COMPLETO DEL PACIENTE A MODIFICAR: ");
        if (pacientes.containsKey(nombre)) {
            Paciente p = pacientes.get(nombre);
            String nombreAux = leer("INGRESE EL NUEVO NOMBRE DEL PACIENTE: ");
            int edadAux = leerInt("INGRESE NUEVA EDAD DEL PACIENTE: ");
            p.setNombre(nombreAux);
            p.setEdad(edadAux);
            pacientes.remove(nombre);
            pacientes.put(nombreAux, p);
        } else {
            System.out.println("EL PACIENTE NO FUE ENCONTRADO");
        }
    }
 
    public static void listar() {
        for (String key : pacientes.keySet()) {
            System.out.println("PACIENTE: " + pacientes.get(key).getNombre() + " EDAD: " + pacientes.get(key).getEdad());
        }
    }
 
    public static void salir() {
        System.out.println("Adios...");
    }
}
 
class Paciente {
 
    private String nombre;
    private int edad;
 
    /**
     * @return the nombre
     */
    public String getNombre() {
        return nombre;
    }
 
    /**
     * @param nombre the nombre to set
     */
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
 
    /**
     * @return the edad
     */
    public int getEdad() {
        return edad;
    }
 
    /**
     * @param edad the edad to set
     */
    public void setEdad(int edad) {
        this.edad = edad;
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar