tengo el siguiente código:
public double[] llegirDades() {
teclat=new Scanner(System.in);
double arrayNotes[]=new double[4];
System.out.println(" A continuacio introdueixi les 5 notes:");
for(int i=0;i<=arrayNotes.length;i++) {
System.out.print(" Nota "+i+": ");
boolean llegit=false;
while(!llegit) {
llegit=teclat.hasNextDouble();
if(llegit) {
arrayNotes[i]=teclat.nextDouble();
}else {
System.out.print("El valor introduit no es un real, torna-ho a provar: ");
teclat.next();
}
}
}
calculaMax();
return arrayNotes;
}
public double calculaMax() {
double MAX=0;
double arrayNotes1[]=new double[4];
arrayNotes1 = llegirDades();
for(int i=1;i<=arrayNotes1.length-1;i++){
for(int j=0;j<arrayNotes1.length;j++) {
if(arrayNotes1[i]<arrayNotes1[j]) {
arrayNotes1[j]=MAX;
}
}
}
imprimir();
return MAX;
}
public void imprimir() {
double max=calculaMax();
System.out.print("La nota maxima es: "+max);
}
Al ejecutar me devuelve el siguiente error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
at test.test.llegirDades(test.java:35)
at test.test.mostrarMenu(test.java:19)
at test.test.main(test.java:12)
La línea 35 coincide con el arrayNotes[i]=teclat.nextDouble()
;
No veo donde está el error. He probado a ir eliminando partes del código, pero sigue fallando y yo sigo sin ver por qué.
imprimir
llama acalculaMax
que en la penúltima línea llama nuevamente aimprimir
.