java

Trying to find average between the numbers of 2 different arrays


I'm trying to find out what's the average between the numbers of 2 different arrays. Well, actually I made it, the thing is, I'm making a verification, to make sure both arrays have the same length. Therefore when I commit the mistake (on purpose) of defining the length of one of them differentely, it shows the error message, and goes back to defining the array length, but when I make the calculous for the average, it returns it wrong, for some reason. Literally, everything works well, except when the function compararValores() is called.

Save me, please.

import java.util.Scanner;

public class MediaVetores {
    public static void main(String[] args) {
        int vec1 [];
        int vec2 [];
        vec1 = lerVetor1();
        vec2 = lerVetor2();
        mediaVetores(vec1, vec2);
    }

    static int lerInteiro(String pergunta){
        System.out.println(pergunta);
        Scanner ler = new Scanner(System.in);
        return ler.nextInt();
    }

    static int [] lerVetor1(){
        int tamVetor = lerInteiro("Quantos valores quer inserir no 1º vetor?: ");
        int vec1 [] = new int [tamVetor];
        for (int i = 0; i < tamVetor; i++){
            vec1 [i] = lerInteiro("Insira o valor do " + (i+1) + "º número do vetor: ");
        }
        return vec1;
    }

    static int [] lerVetor2(){
        int tamVetor = lerInteiro("Quantos valores quer inserir no 2º vetor?: ");
        int vec2 [] = new int [tamVetor];
        for (int i = 0; i < tamVetor; i++){
            vec2 [i] = lerInteiro("Insira o valor do " + (i+1) + "º número do vetor: ");
        }
        return vec2;
    }

    static void mediaVetores(int vec1 [],int vec2 [] ){
        int n1 = 0;
        int c1 = 0;
        int n2 = 0;
        int c2 = 0;
        int total = 0;
        int soma = 0;
        double media = 0.0;
        for (int i = 0; i < vec1.length; i++){
            n1 = n1 + vec1[i];
            c1 ++;
        }
        for (int j = 0; j < vec2.length; j++){
            n2 = n2 + vec2[j];
            c2 ++;
        }
        soma = n1 + n2;
        System.out.println(soma);
        total = c1 + c2;
        System.out.println(total);
        compararVetores(vec1, vec2);
        media = (double) soma / (double) total;

        System.out.println("A média dos valores inseridos nos vetores é de " + media);

    }

   static void compararVetores (int a[], int b[]){
        if (a.length != b.length){
            System.out.println("Os vetores devem ter o mesmo número de valores! Volte a introduzir novos tamanhos de vetores.");
            lerVetor1();
            lerVetor2();

        }
    }

}


Solution

  • First of all you have to simplify your code:

    • Use only one instance of Scanner
    • Use one method to create both arrays
    • Avoid unused variables while calculating median

    And then you do not need the compararVetores() method. In case it's mandatory to have both arrays with the same length, you should ask use once for an array size, and then ask to enter elements of each arrays eparately.

    public static void main(String... args) {
        Scanner scan = new Scanner(System.in);
    
        System.out.print("How many values do you want to insert vector?: ");
        int total = scan.nextInt();
    
        int[] arr1 = createArray(scan, 1, total);
        int[] arr2 = createArray(scan, 2, total);
    
        mediaVectors(arr1, arr2);
    }
    
    private static int[] createArray(Scanner scan, int no, int total) {
        System.out.format("Enter total %d values of vector %d (space or enter are separators): ", total, no);
    
        int[] arr = new int[total];
    
        for (int i = 0; i < arr.length; i++)
            arr[i] = scan.nextInt();
    
        return arr;
    }
    
    private static void mediaVectors(int[] arr1, int[] arr2) {
        int total = arr1.length + arr2.length;
        int sum = Arrays.stream(arr1).sum() + Arrays.stream(arr2).sum();
        double media = (double) sum / (double) total;
    
        System.out.println("Sum of values: " + sum);
        System.out.println("Total values: " + total);
        System.out.format(Locale.ENGLISH, "The average of the values entered in the vectors: %.2f", media);
    }
    

    Output:

    How many values do you want to insert vector?: 4
    Enter total 4 values of vector 1 (space or enter are separators): 1 2 3 4
    Enter total 4 values of vector 2 (space or enter are separators): 5 6 7 8
    Sum: 36
    Total: 8
    The average of the values entered in the vectors: 4.50