Vector error - Arrayindexoutofboundsexception error?

Asked

Viewed 43 times

2

Guys I’m trying to solve this exercise

Develop a program to receive the name of a student with their respective 2 grades, then calculate the student’s average and present at the end the calculated average and the student’s approval status. (approved with average >= 6).

  • Use the code for a class of 30 students.
  • Calculate and show the overall class average
  • Show the highest average in class
  • Show the lowest grade point average

I’ve tried to

public static void main(String[] args) {

    Scanner teclado = new Scanner(System.in);
    String[] aluno;
    float nota1[], nota2[], media[], mediaTurma;
    aluno = new String[30];
    nota1 = new float[30];
    nota2 = new float[30];
    media = new float[30];
    mediaTurma = 0;
    for (int i = 0; i < 30; i++) {
        System.out.println("Digite o nome do Aluno: ");
        aluno[30] = teclado.nextLine();
        System.out.println("Digite o valor da primeira nota: ");
        nota1[30] = teclado.nextFloat();
        System.out.println("Digite o valor da segunda  nota: ");
        nota2[30] = teclado.nextFloat();
        media[30] = (nota1[30] + nota2[30] / 2);
        //mediaTurma += media[i];
        mediaTurma = i / 30;
        System.out.printf("A media do Aluno é %.2f\n", media[30]);

        if (media[30] >= 6) {
            System.out.println("Aluno aprovado Parabens!");
        } else {

            System.out.println("Reprovado, Estude mais!");
        }


    }

}

when compiling I get this error:

Digite o nome do Aluno: 
Bruno
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 30
    at ExercicioQuadro.main(ExercicioQuadro.java:18)

Process finished with exit code 1

Where am I going wrong?

  • 1

    you made a for but is using beyond the fixed value (ex: aluno[30]) is still out of range, the Indice goes 0-29, starts at zero, and in place of the [30] in all variables should be [i]

  • 1

    and this average calculation is wrong, it needs to have the sum of all the values and then divide by 30, this cannot be done within the for, can add inside the for but will only have the final result after the for

1 answer

2

Arrays are indexed to zero (the first position is 0, the second is 1, and so on). As you stated your arrays with size 30, their positions go from zero to 29, but you’re trying to access position 30 (as in aluno[30], for example). And since it is a position that does not exist, the error of ArrayIndexOutOfBoundsException.

How are you making one for with the i ranging from 0 to 29, just do aluno[i].

Although aluno is an array that can store data from multiple students, so I would change the name to alunos (plural, to make it clear what he’s saying). It may sound silly, but give names better help when programming (I did the same for the other arrays).

And I don’t know if you need arrays to save the first and second notes, because it seems that you just need to keep the averages:

Scanner teclado = new Scanner(System.in);
int qtdAlunos = 30; // quantidade de alunos
String[] alunos = new String[qtdAlunos];
float medias[] = new float[qtdAlunos];
float mediaTurma = 0;
for (int i = 0; i < qtdAlunos; i++) {
    System.out.println("Digite o nome do Aluno: ");
    alunos[i] = teclado.nextLine();
    System.out.println("Digite o valor da primeira nota: ");
    float nota1 = Float.parseFloat(teclado.nextLine());
    System.out.println("Digite o valor da segunda  nota: ");
    float nota2 = Float.parseFloat(teclado.nextLine());
    medias[i] = (nota1 + nota2) / 2;
    mediaTurma += medias[i];
    System.out.printf("A media do Aluno é %.2f\n", medias[i]);

    if (medias[i] >= 6) {
        System.out.println("Aluno aprovado Parabens!");
    } else {
        System.out.println("Reprovado, Estude mais!");
    }
}
mediaTurma /= qtdAlunos;
System.out.println("A média da turma é " + mediaTurma);

Actually, I don’t even know if you need arrays - the exercise doesn’t make it clear if it’s to store the data, it just asks to calculate the averages.

As for the rest (show the highest and lowest average in the class), you can do everything in the same loop:

Scanner teclado = new Scanner(System.in);
int qtdAlunos = 30; // quantidade de alunos
float mediaTurma = 0, maiorMedia = Float.MIN_VALUE, menorMedia = Float.MAX_VALUE;
for (int i = 0; i < qtdAlunos; i++) {
    System.out.println("Digite o nome do Aluno: ");
    String nome = teclado.nextLine();
    System.out.println("Digite o valor da primeira nota: ");
    float nota1 = Float.parseFloat(teclado.nextLine());
    System.out.println("Digite o valor da segunda nota: ");
    float nota2 = Float.parseFloat(teclado.nextLine());
    float media = (nota1 + nota2) / 2;
    mediaTurma += media;
    System.out.printf("A média do Aluno é %.2f\n", media);
    if (media > maiorMedia)
        maiorMedia = media;
    if (media < menorMedia)
        menorMedia = media;

    if (media >= 6) {
        System.out.printf("Aluno %s aprovado. Parabéns!\n", nome);
    } else {
        System.out.printf("Aluno %s reprovado.\n", nome);
    }
}
mediaTurma /= qtdAlunos;
System.out.println("\nA média da turma é " + mediaTurma);
System.out.println("A maior média é " + maiorMedia);
System.out.println("A menor média é " + menorMedia);

Browser other questions tagged

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