Vector: Enter 5 names and their heights, then show them all in ascending order. [duplicate]

1

I need to register 5 names and their heights, then show them all in ascending order. The problem is that I can only put the heights in the order, the names are in the order I typed. And also I am not able to use the double in place of the int for the height, because of the error. Here's my code: If I have something very wrong, it's because I'm still a beginner.

public static void main(String args[]) {
    int numero = Integer.parseInt(JOptionPane.showInputDialog("Digite o número de pessoas a ser cadastrado "));
    String vet[] = new String[numero];
    int altura[] = new int[5];
    int aux;

    for (int i = 0; i < vet.length; i++) {
        vet[i] = JOptionPane.showInputDialog("Digite o nome: ", null);
        altura[i] = Integer.parseInt(JOptionPane.showInputDialog("Digite a altura: ", null));
    }

    for (int i = 0; i < altura.length; i++) {
        for (int j = 0; j < (altura.length); j++) {
            if (altura[i] < altura[j]) {

                aux = altura[i];
                altura[i] = altura[j];
                altura[j] = aux;
            }

        }
    }
    for (int i = 0; i < altura.length; i++) {
        System.out.println("Nome " + (i+1) + "º " + vet[i] + altura[i] + "");
    }

}
    
asked by anonymous 14.08.2017 / 05:46

1 answer

1

In almost all situations that have several information associated with each other the ideal is to use classes, which organize and facilitate logic.

If in your example you would register names and heights, and these refer people then a Pessoa class would be a good idea.

This class could look like this:

public class Pessoa {

    private String nome;
    private float altura;

    public Pessoa(String nome, float altura){
        this.nome = nome;
        this.altura = altura;
    }

    public String getNome(){
        return nome;
    }

    public String emTexto(){
        return "Nome: " + nome + "\nAltura: " + altura;
    }
}

The constructor was built to make it easy to create objects of type Pessoa logo with nome and altura . The emTexto method is only meant to have a representation of Pessoa in text and thus be easy to display on the console.

Then main would now look different:

public static void main(String[] args) {
    int numero = Integer.parseInt(JOptionPane.showInputDialog("Digite o número de pessoas a ser cadastrado "));

    Pessoa pessoas[] = new Pessoa[numero]; //agora array de Pessoas

    for (int i = 0; i < pessoas.length; i++) {
        String nome = JOptionPane.showInputDialog("Digite o nome: ", null);
        float altura = Float.parseFloat(JOptionPane.showInputDialog("Digite a altura: ", null));

        //criar a Pessoa e inserir na posição certa do vetor
        pessoas[i] = new Pessoa(nome, altura); 
    }

    //ordena e mostra de uma só vez utilizando sorted() e forEach()
    Arrays.stream(pessoas).sorted(new Comparator<Pessoa>(){
        @Override
        public int compare(Pessoa p1, Pessoa p2) {
            return p1.getNome().compareTo(p2.getNome());
        }
    }).forEach(x-> System.out.println(x.emTexto()));
}

The sort order here as done with an object of a class already has to be customized, so you must receive a Comparator . In this comparator one made comparison of people based on their names.

After sorting, the forEach has been called to scroll through all the ordered people and displays them at the cost emTexto method.

Notes:

  • To enter floats you will need to use the dot decimal separator.
  • To Comparator do not give error you will have to make the corresponding import : import java.util.Comparator ;
14.08.2017 / 14:14