In the method consultas()
, how could I print the student's name and grades?
Until now what it prints to me are memory addresses, and not the name and grades.
Note: The map that receives this method is a map that returns a method called darDeAlta()
which receives the data of a new student.
class Alumno {
private String nombre;
private int matricula;
private double calificaciones[];
Alumno(String nombre, int matricula, double calificaciones[]) {
this.calificaciones = calificaciones;
}
String getNombre() {
return nombre;
}
int getMat() {
return matricula;
}
double[] getCalif() {
return calificaciones;
}
void setCalif(double calif[]) {
calificaciones = calif;
}
}
public class Ejercicio {
Map<Integer, Alumno> alumnos = new TreeMap<>();
public Map darDeAlta() {
Scanner y = new Scanner(System.in);
//boolean b=false;
double d[] = new double[5];
System.out.println("Introduzca el nombre del estudiante");
String n = y.nextLine();
System.out.println("Introduzca su matricula (de 5 digitos)");
int m = y.nextInt();
System.out.println("Introduzca las calificaciones: \n");
for (int i = 0; i < 5; i++) {
System.out.println("Introduzca la calificacion");
d[i] = y.nextDouble();
}
if (alumnos.containsKey(m)) {
System.out.println("Esa matricula ya existe, ingrese otra matricula");
} else {
alumnos.put(m, new Alumno(n, m, d));
System.out.println("Se han dado de alta los datos :)");
System.out.println("Desea ingresar mas alumnos? \n");
String s = y.next();
//if (s.compareTo("si")==0 || s.compareTo("no")==0)
// b = true;
}
return alumnos;
}
public boolean consultas(Map alumnos) {
Scanner y = new Scanner(System.in);
boolean c;
System.out.println("Introduzca la matricula del estudiante a buscar");
int k = y.nextInt();
c = alumnos.containsKey(k);
if (c) {
System.out.println("Los datos son:\n" +"Nombre del alumno: "+ alumnos.get(k) + "Calificaciones: " + alumnos.values());
//System.out.println("Los datos son:\n" +"Nombre del alumno: "+ alumnos.get(k).getNombre() + "Calificaciones: " + al.getCalif());
} else {
System.out.println("El alumno no se encuentra");
}
return true;
}
In your code you have several errors.
First
In your class you
Alumno
are only initializing the variablecalificaciones
, so you will only be able to access information stored in that variable. If you try to access any other variable through the methodsget
, you'll get nothing, since you're not assigning any value to it. The values that you pass to the constructor of the Student class remain in nothing, with the exception of the value of the parametercalificaciones
, since that is passed to the variablecalificaciones
.To assign values to the other variables, you have to initialize them in the constructor.
Second
To obtain the values stored in the variables of the class
Alumno
, you have to access the methodsget
of the variables, otherwise you will only access the Student object and it will show you "memory addresses", as it is happening to you. To avoid that you would have to override the methodtoString()
and within it return aString
in which you concatenate the value of all the variables.To access the values of the variables, use the get methods of these.
With
alumnos.get(k)
you access the object of type Student, so that it is shown to you as anString
override of the methodtoString()
.With these fixes your code would look like this:
Pupil
Exercise
Your class
Alumno
is missing assigning thenombre
y valuesmatricula
in the constructor.Also, you can or should add a method
toString()
to your students class to be able to read the map.Note that the map reading shows you the data as you have defined the
toString
.Then you can read the map like this:
You will have something like this:
If you don't want to implement the method
toString
, you can do it like this:The result will be:
PS: As of Java 8, maps are read like this:
To print a specific student
You ask for the
key
Student usingcontainsKey(keyDelAlumno)
, for example, if you want the data for Student 1:Result: