Adding arrays (vectors) with methods

Asked

Viewed 1,053 times

2

I have an exercise to do: create a method where the user enters the values of 2 vectors, then create a method to sum the values of the 2 vectors and finally a third method that shows the new vector created.

Follow the code created so far:

import java.util.Arrays;
import javax.swing.JOptionPane;

public class Vetor2 {

    public static void main(String[] args) {

        int[] vetorA = new int[5];
        int[] vetorB = new int[5];
        inserirValores(vetorA, vetorB); 
        somaValores(vetorA, vetorB);
        int [] resultado = somaValores(vetorA, vetorB);
        criaNovoVetor(novoVetor);
    }

    public static void inserirValores(int[] a, int[] b) {
        for (int i = 0; i < a.length; i++) {
            a[i] = Integer.parseInt(JOptionPane.showInputDialog(null, "Insira o " + (i + 1) + "o valor do vetor A: "));
            b[i] = Integer.parseInt(JOptionPane.showInputDialog(null, "Insira o " + (i + 1) + "o valor do vetor B: "));
        }
    }

    public static int somaValores(int[] a, int[] b) {
        int[] vetorC = new int[5];
        for (int i = 0; i < 5; i++) {
            vetorC[i] = a[i] + b[i];
        }
        return vetorC[5];
    }

    public static void criaNovoVetor(int [] novoVetor) {
        JOptionPane.showMessageDialog(null, "O novo vetor é: " + Arrays.toString(novoVetor));
    }
}

I’m in two trouble:

1 - I cannot record the result of the sum of a third vector (either using the somaValue method itself, or trying to pass to the method creatinNovoVetor - I tried to solve only with 2 methods first because I thought it would be easier, but anyway need to do with all three). That error occurs int cannot be converted to int[]. I don’t understand since on the left side is a new vector and on the right the sum of 2 vectors, for me the two would be int[].

2 - The line Return vetorC[5] overflows the array, I don’t know why since it is the same size as the other two.

  • Which way out of somaVetores? An integer with all the sum of the 10 elements or a vector whose 5 elements are the sums of the respective box of a and b?

1 answer

1

The first problem is on this line:

int [] resultado = somaValores(vetorA, vetorB);

The method somaValores() returns int and not int[].

Change the return type to int[] and instead of doing return vetorC[5]; do return vetorC;


The second problem is why arrays are zero based. That means their first position is 0 and not 1, that is, an array of 5 positions does not go from 1 to 5, but from 0 to 4.
In doing return vetorC[5]; the sixth position will be sought and as it does not exist, the exception ArrayIndexOutOfBoundsException is launched.

The right thing would be return vetorC[4];, but as this excerpt will be removed, the problem will be removed as well.


Tip: in the method somaValores() not "force" as far as your for must go. Use array size as parameter. See:

Instead of doing like this:

for (int i = 0; i < 5; i++)

Do so:

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

As is already done in the method inserirValores().

  • Thanks for the considerations Igor. But if I put "Return Vetorc." It gives the same error int[] cannot converted to int, I believe because if vector C is vector[], I can’t even ask to return it as vector C only. The size of the vector, even used with length before, but it bursts the same way and as my variable "i" start at zero, it is within the size of 5 positions.

  • @Luisfernando sorry. I updated the reply. Do return vetorC[];

  • rs, already tried too, ai aparece: . class expected / Unexpected type: required: value, found class;

  • @Luisfernando I returned the answer to what was previously. What I said is wrong. I’m trying to understand what is happening.. u changed the type of return? which line of the error?

Browser other questions tagged

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