How to enter arrays by keyboard and how to initialize them in the main? Java Poo

0

Good I'm doing a program where I must (enter the name and the note of some students) make 2 arrays one of the String type and another of the float type. I have a problem in the main when I want to enter the student data by console. First I put the Scanner then I declare two arrays of the type String and float, but they tell me that I must initialize it and eclipse tells me that I should put them equal to null, but I get a warning in this part:

nombre[i] = teclado.next();
nota [i]= teclado.nextFloat();

that tells me that I can not put them null and when I do it and input the first data I get:

Exception in thread "main" java.lang.NullPointerException     at arrays_alumnos.PruebaAlumno.main (PruebaAlumno.java:20)

I do not know if the problem is when I put the arrays on keyboard.next (); / teclado.nextFloat (); or why not initialize the variables and in doing so I put them null and it's wrong.

public class Alumnos {

private String nombreAlumno[] ;
private float nota[];

public Alumnos() {

    this.nombreAlumno = new String [5] ;
    this.nota = new float [5];
}

public Alumnos(String nombre[] , float[] nota) {
    this.nombreAlumno = nombre ;
    this.nota= nota;

}

public String[] getNombre() {

    return this.nombreAlumno;
}

public void setNombre(String [] nombreAlumno ) {

    this.nombreAlumno = nombreAlumno;
}

public float [] getNota() {

    return this.nota;
}

public void setNota(float [] nota) {

    this.nota = nota ;
}

public void verificacionDeNota() {

    for (int i = 0; i < nombreAlumno.length; i++) {

        if(nota[i]>=7.0) {

        System.out.println("Alumno : " +nombreAlumno + " Promocionado.");

    }

    if (nota[i] >=1.0 && nota[i] <=3.0) {

        System.out.println("Alumno : " +nombreAlumno +" Reprobado.");

    }

    if(nota[i]>=4 && nota[i]<=6) {

        System.out.println("Alumno : " +nombreAlumno +" Aprobado.");
    }

    }


}

}

import java.util.Scanner;

public class TestStudent {

public static void main(String[] args) {

    Scanner teclado = new Scanner (System.in);

    String nombre[] = null ;
    float nota[]  = null;



    for ( int i = 0; i < 5; i++) {

        System.out.println((i+1)+".Ingrese nombre : ");
        nombre[i] = teclado.next();
        System.out.println((i+1)+".Ingrese nota : ");
        nota [i]= teclado.nextFloat();

        Alumnos curso1 = new Alumnos (nombre , nota);


    }



}

}

    
asked by computer96 08.12.2018 в 03:52
source

1 answer

1

In your class, your attributes are treating them as fixes, that means you'll have a float array and a String array for each instance of your object. I recommend you just treat them as float and String

public class Alumno {
    private String nombreAlumno[] ;
    private float nota[];
    .... //metodos

}

your code would stay that way for the attributes.

public class Alumno {
    private String nombreAlumno;
    private float nota;
    .... //metodos

}

your constructor, also has the same problem, you should stay like this so that you do not have any error, with respect to your other constructor I think that this one.

public Alumno(String nombreAlumno , float nota) {

   this.nombreAlumno = nombreAlumno ;
   this.nota = nota;
}

the declaration of your methods, also change it, so that they remain that way.

public String getNombre(){...}
public void setNombre(String nombreAlumno ) {...}
public float getNota() {...}
public void setNota(float nota) {...}

Now your main, the same as when you capture data, you are treating it to the variable name and note as fixes. The best thing would be to just make it normal. That fits you like this

public static void main(String[] args) {

    Scanner teclado = new Scanner (System.in);

    String nombre = null ;
    float nota  = null;

    System.out.println((i+1)+".Ingrese nombre : ");
    nombre = teclado.next();
    System.out.println((i+1)+".Ingrese nota : ");
    nota = teclado.nextFloat();

    Alumno alumno1 = new Alumnos (nombre , nota);

}

now if you want to store the data of a AULA , I would recommend you create another class that stores several STUDENTS

public class Aula{
    private String nombreAula;
    private HashSet <Alumnos> listaAlumnos = new HashSet<Alumnos>();

    public Aula(String nombreAula){
        this.nombreAula = nombreAula;
    }

   public void agregarAlumno(Alumnos al){
       listaAlumnos.add(al);
    }
}

in which the addAllum (Students to) function will allow you to add students whenever you want.

public static void main(String[] args) {

    Scanner teclado = new Scanner (System.in);

    String nombre = null ;
    float nota  = null;
    Aula mate = new Aula("MATEMATICAS");


    System.out.println(".Ingrese nombre : ");
    nombre = teclado.next();
    System.out.println(".Ingrese nota : ");
    nota = teclado.nextFloat();

    Alumno alumno1 = new Alumnos (nombre , nota);
    mate.agregarAlumno(alumno1);
 }

If you want, you put it between a for and you ask how many students you will enter.

    
answered by 08.12.2018 в 04:29