How to alphabetically order a string array in java

1

I have a problem when ordering my array string names, try using the Arrays.sort (); but my code did not work here.

package mainclass;
import java.util.Scanner;

public class Personas {
   public static Scanner leer=new Scanner(System.in);
   public static String nombre[]=new String[5];
   public static int edad[]=new int[5];
   public static double altura[]=new double[5]; 
   public static int peso[]=new int[5];
   public static String sexo[]=new String[5];
   public static int Contador;
   public static String ComparaNom;

   public static void registrar(){

       if(Contador<5)
       {
       System.out.println("Nombre: ");
       nombre[Contador]=leer.next();
       System.out.println("Edad: ");
       edad[Contador]=leer.nextInt();
       System.out.println("Altura: ");
       altura[Contador]=leer.nextDouble();
       Contador++;

       }

       else
       {
           System.out.println("No hay mas espacio...");
       }
   }
   public static void borrar(){
       System.out.println("Borrar: "); ComparaNom=leer.next();
       for(int i=0;i<Contador;i++){
           if(ComparaNom.equals(nombre[i])){
               for(int z=i;z<Contador;z++){
                   nombre[z]=nombre[z+1];
                   edad[z]=edad[z+1];
                   altura[z]=altura[z+1];
               }
           }
       }Contador--;
   }
   public static void modificar(){
       System.out.println("Modificar: "); ComparaNom=leer.next();
       for(int i=0; i<Contador;i++){
           if(ComparaNom.equals(nombre[i])){
               System.out.println("Nombre: ");
               nombre[i]=leer.next();
               System.out.println("Edad: ");
               edad[i]=leer.nextInt();
               System.out.println("Altura: ");
               altura[i]=leer.nextDouble();
           }
       }
   }
   public static void busqueda(){
       System.out.println("Buscar: "); ComparaNom=leer.next();
       for(int i=0;i<Contador;i++){
           if(nombre[i].equals(ComparaNom)){
               System.out.println("Nombre: "+nombre[i]);
               System.out.println("Edad: "+edad[i]);
               System.out.println("Altura: "+altura[i]);
           }
       }
   }
   public static void general(){

       for(int i=0;i<Contador;i++){
           System.out.println("");
           System.out.println("Nombre: "+nombre[i]);
           System.out.println("Edad: "+edad[i]);
           System.out.println("Altura: "+altura[i]);
       }
   }
}
    
asked by Pedro Robles 01.10.2017 в 01:54
source

1 answer

1

The reason you are having these problems is that the nextInt(); method, the nextDouble(); method, are leaving "residues" in the buffer. Specifically the line break '\ n' since it is not part of the number and in the next data entry, before entering them, it detects that there is data in the buffer and takes it. I'm sure you've noticed that in some sentences you do not enter data and that it automatically jumps to the next instruction. Reason of the commented previously. For this reason, the statement Arrays.sort(nombre); returns a failure based on the fact that not all variables are correctly initialized.

To solve this, before the statement that automatically jumps without entering data, you must clean the buffer, introducing previously to where the leer.nextLine();

statement "jumps"

Read more

    
answered by 01.10.2017 / 10:15
source