Put vectors in order. JAVA

Asked

Viewed 914 times

2

I need to put five pesos in order from the smallest to the largest. Defining the numbers instead for example: int vet[] = {3,4,2,8,7,1}; it runs right, but I need to do it this way: int vet[] = new int [5]; that way he’s only getting 3 numbers like this showing in the image. follows the code:

Código

int vet[] = new int [5];

int aux;

boolean controle;

for(int i = 0; i < vet.length; i++ ) {

    vet[i] = Integer.parseInt(JOptionPane.showInputDialog("Digite o peso: "));

    controle = true;

    for(int j = 0; j < (vet.length -1); j++) {

        if(vet[j] > vet[j+1]) {

            aux = vet[j];
            vet[j] = vet[j + 1];
            vet[j + 1] = aux;
            controle = false;
        }

    }

    if(controle) {

        break;

    }

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

}
  • 1

    It would not be better to divide this into two parts, where the first reads the values and places them in the array and the second sorts them?

2 answers

1


Well I separated her For that completes the Vector of For that organizes since you need has a value for exchange when it does this reading vet[j] > vet[j+1]) you have a value in j but j + 1 no, you have zero start

  public static void main(String args[]) {

        int vet[] = new int [5];

        int aux;

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


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

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

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


    }
  • Hello, I put your code to run and it’s getting all 5 numbers straight, but now it’s not putting in order. You know the possible reason?

  • Fixed and took, see if it works now

  • I noticed that you are asking for the weight I would use another type instead of Int, since it does not accept floating point

  • Hello Bruno, with your new correction the code is running right! Thank you for your attention and help. You would recommend me to use the Double?

  • Yes, Double or float

  • Thanks for the recommendation!

Show 1 more comment

1

I am responding as an alternative to Bruno’s solution, using Java 8 and Streams. And while I may not be as educational is certainly interesting.

The reading part would be equal and separate from the ordering:

int vet[] = new int [5];

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

Now to order and show would do so:

Arrays.stream(vet).sorted().forEach(x -> System.out.println(x));

The .stream(vet) calf the Stream of integers that will be a IntStream. Then the .sorted() orders the Stream and finally the forEach corresponds to the cycle that was done at the end to display each element.

  • Thanks for the tip!

Browser other questions tagged

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