Ver Mensaje Individual
  #1 (permalink)  
Antiguo 30/05/2013, 13:25
Avatar de erozwashere
erozwashere
 
Fecha de Ingreso: noviembre-2012
Ubicación: mex
Mensajes: 176
Antigüedad: 11 años, 5 meses
Puntos: 0
Busqueda secuencial en java

Hola, estoy tratando de hacer una busqueda secuencial, pero no logro que se guarden los datos en el arreglo para despues mostrarlos en la consulta, alguna idea de cual es mi error?

Código Java:
Ver original
  1. package javaapplication12;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5.  
  6. public class JavaApplication12 {
  7.  
  8.     static BufferedReader br = null;
  9.     static String N= "";
  10.     static int n=0;
  11.    
  12.    
  13.    
  14.     static String[] no_ = new String[50];
  15.     static String[] ap_ = new String[50];
  16.     static String[] ma_ = new String[50];
  17.     static String[] ca_ = new String[50];
  18.    
  19.     public static void main(String[] args) {
  20.         br = new BufferedReader(new InputStreamReader(System.in));
  21.  
  22.         do{
  23.             menu();
  24.         }while(Integer.parseInt(N)!=4);
  25.     }
  26.  
  27.     public static void menu(){
  28.          do{
  29.             System.out.println("Selecciona una de las opciones del menú: \n "
  30.                                     + "1- INSERTAR \n "
  31.                                     + "2- CONSULTAR "
  32.                                     + "\n 3- ELIMINAR \n "
  33.                                     + "4- FINALIZAR");
  34.             try{
  35.                  N = br.readLine();
  36.             }catch(Exception e){
  37.                 e.printStackTrace();
  38.             }
  39.         }while(!esEntero(N) || conversor(N)<=0 || conversor(N)>=5 );
  40.  
  41.          switch(Integer.parseInt(N)){
  42.             case 1:
  43.                 insertar();
  44.                 break;
  45.             case 2:
  46.                 consultar();
  47.                 break;
  48.             case 3:
  49.                 eliminar();
  50.                 break;
  51.             case 4:
  52.                 imprimir();
  53.                 break;
  54.         }
  55.     }
  56.  
  57.    
  58.     public static void insertar(){
  59.  
  60.             if(n<50){
  61.                 System.out.println("Leyendo datos de la persona: " + (n+1));
  62.  
  63.             try{
  64.                  System.out.println("Ingresa el nombre: ");
  65.                  no_[n] = br.readLine();
  66.                
  67.             }catch(Exception e){
  68.                 e.printStackTrace();
  69.             }
  70.  
  71.             try{
  72.                 System.out.println("Ingresa el Apellido Paterno: ");
  73.                 ap_[n] = br.readLine();
  74.             }catch(Exception e){
  75.                 e.printStackTrace();
  76.             }
  77.  
  78.             try{
  79.                  System.out.println("Ingresa la Matricula: ");
  80.                  ma_[n] = br.readLine();
  81.             }catch(Exception e){
  82.                 e.printStackTrace();
  83.             }
  84.             try{
  85.                  System.out.println("Ingresa la Calificacion: ");
  86.                  ca_[n] = br.readLine();
  87.             }catch(Exception e){
  88.                 e.printStackTrace();
  89.             }
  90.  
  91.             n++;
  92.             }else{
  93.                 System.out.println("El vector esta lleno, elimina personas para poder insertar");
  94.             }
  95.  
  96.     }
  97.  
  98.     public static void eliminar(){
  99.             String nombre="";
  100.             int encontrados=0;
  101.  
  102.         if(n>0){
  103.  
  104.             try{
  105.                     System.out.println("Ingresa el Nombre : ");
  106.                     nombre = br.readLine();
  107.                 }catch(Exception e){
  108.                     e.printStackTrace();
  109.                 }
  110.  
  111.             for(int i=0; i<n; i++){
  112.                 if(no_[i].equals(nombre)){
  113.                     encontrados++;
  114.                     for(int j=i; j<n; j++){
  115.                         no_[j]=no_[j+1];
  116.                         ap_[j]=ap_[j+1];
  117.                         ma_[j]=ma_[j+1];
  118.                         ca_[j]=ca_[j+1];
  119.                     }
  120.                     i--;
  121.                     n--;
  122.                 }
  123.             }
  124.             if(encontrados>0){
  125.                 System.out.println("Nombre encontrado, procediendo a eliminar!!!!!. ");
  126.             }else{
  127.                 System.out.println("Nombre NO localizado!!!!!. ");
  128.             }
  129.         }else{
  130.             System.out.println("No hay elementos a eliminar.");
  131.         }
  132.  
  133.     }
  134.  
  135.     public static void consultar(){
  136.         if(n>0){
  137.             String nombre="";
  138.             int eureka=0;
  139.  
  140.             try{
  141.                     System.out.println("Ingresa el Nombre : ");
  142.                     nombre = br.readLine();
  143.                 }catch(Exception e){
  144.                     e.printStackTrace();
  145.                 }
  146.  
  147.             for(int i=0; i<n; i++){
  148.                 if(no_[i].equals(nombre)){
  149.                     eureka=1;
  150.                 }
  151.             }
  152.             if(eureka==1){
  153.                 System.out.println("Nombre encontrado!!!!!. ");
  154.             }else{
  155.                 System.out.println("Nombre NO localizado!!!!!. ");
  156.             }
  157.         }
  158.         else{
  159.             System.out.println("No hay elementos en la lista. ");
  160.         }
  161.  
  162.     }
  163.  
  164.     public static boolean esEntero(String cad) {
  165.         for(int i = 0; i<cad.length(); i++)
  166.             if( !Character.isDigit(cad.charAt(i)) )
  167.         return false;
  168.  
  169.         return true;
  170.     }
  171.  
  172.     public static int conversor(String x){
  173.         int valor=0;
  174.  
  175.         try{
  176.             valor= Integer.parseInt(x);
  177.         }catch(NumberFormatException e){
  178.             System.out.println("Valor invalido");
  179.         }
  180.  
  181.         return valor;
  182.     }
  183.  
  184.     public static void imprimir(){
  185.         if(n>0){
  186.             System.out.println("Nombre  Apellido Paterno   Matricula   Calificacion");
  187.             for(int i=0; i<n; i++){
  188.                 System.out.print(i+1 + ".- " + no_[i] + "\t");
  189.                 System.out.print(ap_ [i] + "\t     ");
  190.                 System.out.print(ma_ [i] + "\t");
  191.                 System.out.print(ca_ [i] + ".");
  192.                 System.out.println();
  193.             }
  194.         }
  195.     }
  196.  
  197. }