Vector: Register 5 names and their heights, then show all in ascending order.

Asked

Viewed 378 times

1

I need to register five names and their heights, then show them all in ascending order. The problem is I can only put the heights in order, the names are in the order I typed. And also I’m not able to use the double in place of the int for the height, because of the error. If there’s something really wrong, it’s because I’m still new.

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] + "");
    }

}
  • Thank you Carlos heuberger, I used aux2 in the code and it worked!

1 answer

1


In almost all situations that have various 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 class Pessoa would be a good idea.

This class could then stay 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 facilitate the creation of type objects Pessoa just with the nome and altura. The method emTexto serves only to have a representation of the Pessoa text and be so easy to display on the console.

Then the 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 part here as it is done with an object of a class already has to be customized, and so it has to receive a Comparator. In this comparator did the comparison of people based on their names.

After the ordination was made called the forEach to go through all ordered people and show them at the cost method emTexto.

Notes:

  • To enter floats you have to use the decimal point separator.
  • Towards the Comparator no mistake will have to do the import correspondent: import java.util.Comparator;
  • Hello, as I pointed out, I was unable to use the double, but with your tip, the float worked perfectly! I couldn’t use her other several lines of code because I don’t understand much about them, but I will analyze the news that she brings to me as a way of study. Thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.