I'm going crazy.
I have a Person class:
public class Persona {
private String nombre;
private String apellidos;
private int edad;
public Persona(String nombre, String apellidos, int edad) {
this.nombre = nombre;
this.apellidos = apellidos;
this.edad = edad;
}
public String getNombre() {
return nombre;
}
public String getApellidos() {
return apellidos;
}
public int getEdad() {
return edad;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public void setApellidos(String apellidos) {
this.apellidos = apellidos;
}
public void setEdad(int edad) {
this.edad = edad;
}
}
And I have a student class that extends from person
public class Estudiante extends Persona{
private String codEstudiante;
private int notaFinal;
public Estudiante(String nombre, String apellidos, int edad, String codEstudiante, int notaFinal) {
super(nombre, apellidos, edad);
this.codEstudiante = codEstudiante;
this.notaFinal = notaFinal;
}
public String getCodEstudiante() {
return codEstudiante;
}
public int getNotaFinal() {
return notaFinal;
}
public void setCodEstudiante(String codEstudiante) {
this.codEstudiante = codEstudiante;
}
public void setNotaFinal(int notaFinal) {
this.notaFinal = notaFinal;
}
public void metodoMostrarEsdutidantes(){
System.out.println("Nombre " + getNombre() +
"\nApellidos: " + getApellidos() +
"\nEdad: " + getEdad() +
"\nCódigo Estudiante: " + getCodEstudiante() +
"\nNota Final: " + getNotaFinal());
}
}
Could someone tell me how I can make this method work that should record the data in the arrayList ?
public static void metodoRecogerDatos(){
System.out.println("Escroba el nombre del Estudiante: ");
String nombre = teclado.nextLine();
System.out.println("Escroba los Apellidos: ");
String apellidos = teclado.nextLine();
System.out.println("Cual es la edad del estudiante: ");
int edad = teclado.nextInt();
teclado.nextLine();
System.out.println("Su código de Estudiante es: ");
String codigoEstudiante = teclado.nextLine();
System.out.println("Escroba el nombre del EstudianteNota Final obtenida: ");
int notaFinal = teclado.nextInt();
teclado.nextLine();
//Estudiantes.add();
//lo estoy haciendo así, pero al llamar al método que imprime los datos, me da el valor null
Estudiante miEstudiante = new Estudiante(nombre, apellidos, edad, codigoEstudiante, notaFinal);
}
Can someone tell me how I can call the method ShowStudents() method from the main class?
When I try to instantiate an object of the student class, it gives me an error, and it tells me that the constructor parameters of the class are missing, so by default I gave the option that netBeans gives me to correct it, and it has added an empty constructor but I don't know if it's correct
My main class is this:
public class Principal {
ArrayList<Estudiante> Estudiantes = new ArrayList<Estudiante>();
static Scanner teclado = new Scanner(System.in);
Estudiante miEstudiante = new Estudiante();
public static void main(String[] args){
int opcion = 0;
do{
opcion = metodoMostrarMenu();
switch (opcion){
case 1:
metodoRecogerDatos();
break;
case 2:
metodoMostrarDatos();
break;
case 3:
System.exit(0);
break;
}
} while (opcion != 3);
}
public static void metodoRecogerDatos(){
System.out.println("Escroba el nombre del Estudiante: ");
String nombre = teclado.nextLine();
System.out.println("Escroba los Apellidos: ");
String apellidos = teclado.nextLine();
System.out.println("Cual es la edad del estudiante: ");
int edad = teclado.nextInt();
teclado.nextLine();
System.out.println("Su código de Estudiante es: ");
String codigoEstudiante = teclado.nextLine();
System.out.println("Escroba el nombre del EstudianteNota Final obtenida: ");
int notaFinal = teclado.nextInt();
teclado.nextLine();
//Estudiantes.add()
Estudiante miEstudiante = new Estudiante(nombre, apellidos, edad, codigoEstudiante, notaFinal);
}
public static int metodoMostrarMenu(){
System.out.println("---------------------------------------------------");
System.out.println("Que función desea realizar");
System.out.println("---------------------------------------------------");
System.out.println("1 - Introducir estudiantes");
System.out.println("2 - Ver Estudiantes");
System.out.println("3 - Cerrar");
System.out.println("---------------------------------------------------");
System.out.println("Su opción es: ");
System.out.println("---------------------------------------------------");
int opcion = teclado.nextInt();
teclado.nextLine();
return opcion;
}
private static void metodoMostrarDatos() {
Estudiante miEstudiante = new Estudiante();
System.out.println("Los estudiantes matriculados son: ");
miEstudiante.metodoMostrarEsdutidantes();
}
}
The case is that the code does not give any error, but when executing the program and entering the data, when printing it, everything is null
I am very lost. I would appreciate information. Thank you
in your main class I see some things that don't make sense, you should have something like this:
The explanation is simple, you create a student with the data that is inserted, you add it to the list and then you go through it, retrieving the objects and displaying them based on the position they occupy in the list.