Java - Caso 3 no me entra en el bucle y el caso 5 no me ordena bien

 
Vista:

Caso 3 no me entra en el bucle y el caso 5 no me ordena bien

Publicado por celia (3 intervenciones) el 16/02/2020 13:14:33
El caso 3 ha de sacar por pantalla cuántos empleados hay con el mismo primer apellido (el que elija el usuario).El caso 5 ha sacar por pantalla todos los empleados del centro ordenados alfabéticamente.
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
import java.util.Arrays;
import java.util.Scanner;
 
public class Banco2 {
	public static void main(String[] args) {
		//Scanner construction
		Scanner Entrada=new Scanner(System.in);//Scanner used to read Strings
		Scanner ent=new Scanner(System.in);//Scanner used to read Integer variables
		//----------------------------------------------------------------------//
		//Variables used for the problem
		int sucursales=0;
		int options;
		int numsucursal;//Variable used for the case 1
		int numbempleados;//Variable used for the case 1
		int numeroempleado=0;
		String surname;
		String empleados;//Variable used for the case 2
		String Apellido;//Varible used for the case 3
		int numeroempleadototal=0;//Variable used as a counter in the case 4
 
		//--------------------------------------------------------------------------------//
		//We ask the user for the number of branches
			System.out.println("Introduce el numero de sucursales");
			sucursales=ent.nextInt();
		//We read the number of branches that the user introduces
		//Construction of our multidimensional array
		String banco [][]=new String[sucursales][];
		// i rows, j columns
		//In this loop we fill in the position
		for (int i=0;i<banco.length;i++) {
				System.out.println("Introduceme la cantidad de empleados que trabajan en la sucursal " + ( i+1 ));
				numeroempleado=ent.nextInt();
			//We assign it to the object bank
			banco[i]=new String[numeroempleado];
		}
		//We are going to fill in the array
		for (int i=0;i<banco.length;i++) {
			for(int j=0;j<banco[i].length;j++) {
					System.out.println("Introduceme el nombre de los empleados " + ( i+1));//We use i+1 to know the branches that the user is filliing in
					banco[i][j]=Entrada.nextLine();
			}
		}
		//--------------------------------------------------------------------------------------------------------//
		do {
		//Menu options
		System.out.println("Ingrese una de las opciones que desea hacer");
    	System.out.println("1.Sacar por pantalla todos los empleados de unas sucursales");
    	System.out.println("2.Sacar por pantalla a qué sucursal pertenece un empleado");
    	System.out.println("3.Sacar por pantalla cuántos empleados hay con el mismo primer apellido");
    	System.out.println("4.Sacar por pantalla cuántos empleados hay en total en el Banco.");
    	System.out.println("5.Saca por pantalla todos los empleados del centro ordenados alfabéticamente.");
    	//We read the option introduced
    	options=ent.nextInt();
    	switch(options) {
    		case 1:
    			System.out.println("Introduce la sucursal del empleado");
    			numsucursal=ent.nextInt();//We read the branch introduced by the user
    			System.out.println("Introduce el numero del empleado");
    			numbempleados=ent.nextInt();//We introduce the number of the employee that we want to know
    			//We print out our multidimensional array formed by the number of branch and number of employees, taking into account that the number of the branch as it moves will disminish a position
    			System.out.println("El empleado es " + banco [numsucursal-1] [numbempleados]);
    		break;
    		case 2:
    			System.out.println("Dime el nombre del empleado");
    			empleados=Entrada.nextLine();//We read the employee that the user has introduced
    			//We go through the array to know the worker of the branch
    			for (int i=0;i<banco.length;i++) {
    				for (int j=0;j<banco[i].length;j++) {
    					//We make and if to compare the employee name introduced by the user with the employees introduced previously before the menu options
    					//equalsIgnoreCase  ignores the case while comparing two strings.
    					if(empleados.equalsIgnoreCase(banco[i][j])) {
    						//We print out the branch that the employee belongs to
    					System.out.println("El empleado pertenece a la siguiente sucursal: " + (i + 1));
    				}//end of the second for
    			}//end of the first for
    		}
    		break;
    		case 3:
    			//We ask the user for the surname
    			System.out.println("Dime el apellido del empleado");
    			//We read the surname introduced by the user
    			Apellido=Entrada.nextLine();
    			//We do two loops in order to go through the array
    			for(int i=0;i<banco.length;i++) {
    				//No me entra al bucle
    				for (int j=0;j<banco[i].length;j++) {
    					surname=banco[i][j].substring(banco[i][j].indexOf(""),+1);
    					//We assign it to the variable Apellido
    					surname=Apellido.substring(0,Apellido.indexOf(""));
    					if(Apellido.equalsIgnoreCase(surname)) {
    						System.out.println("Los empleados con el apellido que quieres saber son: " +banco[i][j]);
    					}
    				}
    			}
    		break;
    		case 4:
    				//We use to loops to go through the array
    				for (int i=0;i<banco.length;i++){
    					for (int j=0;j<banco[i].length;j++) {
    						//We create a new variable used as a counter of the positions of the array
    						numeroempleadototal ++;
    						//We print out the number of the counter
    						System.out.println("Este es el numero total de empleados que hay en las sucursales " + numeroempleadototal);
    				}
    				}
    		break;
    		case 5:
    			//We create the array with the names to order
    			String ordernames[] = new String[numeroempleadototal];
    			Arrays.sort(ordernames);
    			for(int i=0;i<ordernames.length;i++) {
    				System.out.println("Nombre"+ordernames[i]);
    			}
 
    			break;
    		default:
    		System.out.println("Ha elegido usted la opcion incorrecta");
    		break;
    	}
		}while(options!=6);
	}//end of main
}//end of class
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 Rodrigo
Val: 2.041
Plata
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Caso 3 no me entra en el bucle y el caso 5 no me ordena bien

Publicado por Rodrigo (623 intervenciones) el 16/02/2020 15:24:53
No es claro para mi la intencion de la linea 87 y siguientes.
El metodo substring recibe 2 numeros que son los indices dentro de un string, decir +1 como segundo parametro hara que se llegue hasta el 2do caracter.
Compruebalo imprimiendo el valor de surname justo despues de la linea 87.

En la linea 89, haces que lo que se asigno justo antes se pierda. Podrias borrar la linea 87 y seria el mismo resultado.
Aparentemente tomas un substrings del string apellido para luego comprobar, en la linea siguiente si el substring que tomaste es igual al string completo.
Definitivamente no estas haciendo lo que dice el comentario de la linea 88, pues la variable Apellido no esta siendo asignada.
Tal vez estan cambiadas de lugar?

En el case 5 no hay datos para ordenar.
Se crea un array, pero no se ingresan datos a el.
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