¿Como usar el while con arrays?
Tengo que hacer un programa en el que debo ingresar nombres y notas, dentro de un ciclo while y este termina cuando ingreso una nota igual -1. Pero cuando voy a ejecutar el programa me salta :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException : 0
at NotasYNombres.main(NotasYNombres.java:12)
esta parte es el renglón 12 while(notas[i]!=-1) , no se como poner la condicion en el while para que no me salte ese mensaje.
import java.util.Scanner;
public class NotasYNombres{
public static void main(String [] args){
Scanner teclado = new Scanner (System.in);
int i =0;
int contadorParaFor=0;
int notas[]=new int [i];
String nombres[] =new String [i];
while(notas[i]!=-1){
i++;
contadorParaFor++;
System.out.println(i+".Ingrese nombre : ");
nombres[i]= teclado.next();
System.out.println(i+".Ingrese nota : ");
notas[i]=teclado.nextInt();
}
for( i= 0 ; i<=contadorParaFor ; i++){
if(notas[i]>7){
System.out.println("-"+nombres[i]);
}
}
}
}
1 Respuesta
Respuesta de Stanimir Iankov Stantchev