Arrayindexoutofboundexception error when trying to store values in the array

Asked

Viewed 42 times

-2

I am trying, through the Scanner, to take the values typed by the user, to store in a vector and finally, to average the values typed, in the case the average of a student’s grades. However, I’m having this array exception outside the index, and I’m not able to identify what’s wrong with the code.Below is my class containing my methods and my class containing my main, respectively.

Class of methods

package classeexercicios;

public class MediaAluno {

    double vetorNotas [] = new double[5];
    double media;
    double total = 0;
    int i;

    double receberNotas() {
        if(vetorNotas[i] >= 0 || vetorNotas[i] <= 100) {
            for(i = 0;i < vetorNotas.length;i++) {
                total = total + vetorNotas[i];
            if(vetorNotas[i] < 0 || vetorNotas[i] > 100) {
                System.out.println("Insira uma nota valida entre o intervalo de 0 a 10");
            }
        }
    }
        return i;
    }
    double calcularMedia() {
        total = total + vetorNotas[i];
        return media = (total / vetorNotas.length);

    }

}

Class containing the Main

package classeexercicios;

import java.util.Scanner;

public class MediaAlunoTeste {

    public static void main(String[] args) {

        Scanner teclado = new Scanner(System.in);

        MediaAluno nota = new MediaAluno();
        for(int i = 0;i < 5;i++) {
        System.out.print("Insira a nota do aluno:");
            nota.vetorNotas[i] = teclado.nextDouble();

            nota.receberNotas();
        }

        nota.calcularMedia();

        System.out.println();


        teclado.close();
    }

}

1 answer

0


It would be better to separate the class from the execution, you mixed things up a bit.

you are trying to add the total to the vector Notas at position 5?

double calcularMedia() {
                        //aqui
        total = total + vetorNotas[i];
                              // esse vetorNotas.length é apenas um 5 poderia utilizar o índice
        return media = (total / vetorNotas.length);

    }

If you already had the total would only be divided by the number of times passed in the index.

// seu pacote

import java.util.Scanner;

public class MediaAlunosNotasArray {

    double vetorNotas [] = new double[5];
    double total = 0;
    int i;
    Scanner entrada = new Scanner(System.in);

    double receberNotas() {
            for(i = 0; i < vetorNotas.length;i++) {
                System.out.println("Insira uma nota valida entre o intervalo de 0 a 10");
                vetorNotas[i] = Double.parseDouble(entrada.next());
                if(vetorNotas[i] > 0 && vetorNotas[i] <= 100) {
                    total += vetorNotas[i];
                } else {
                    i--;
                    vetorNotas[i] = vetorNotas[i-1];
                }

            }
        return total;
    }
    void calcularMedia() {
        System.out.println("media é " + total / i);
    }

    public static void main(String[] args) {
        MediaAlunosNotasArray alunos = new MediaAlunosNotasArray();
        alunos.receberNotas();
        alunos.calcularMedia();
    }
}

your problem has become a little vague in the description, if my answer is not valid I can edit.

Browser other questions tagged

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