I'm trying to get the function infoEst() to iterate over an array of objects and print out the object's name and location. It iterates fine but it seems like every index in the array is the exact same as the last entry.
Am I creating the array of objects incorrectly?
import java.util.Scanner;
public class Estudiante {
static String nombre= "", promedio = "", pueblo = "", entrada = "";
static int edad;
static Estudiante[] objArray = new Estudiante[4];
static Scanner input = new Scanner(System.in);
Estudiante(String nom, int ed, String prom, String pueb) {
this.nombre = nom;
this.edad = ed;
this.promedio = prom;
this.pueblo = pueb;
}
static void infoEst(String n) {
for (int i = 0; i < objArray.length; i++) {
if (objArray[i].nombre == n) {
System.out.println(objArray[i].nombre + ", " + objArray[i].pueblo);
}
}
}
public static void main(String[] args) {
objArray[0] = new Estudiante("Joshua", 20, "A", "Aguadilla");
objArray[1] = new Estudiante("Liliana", 21, "A", "Isabella");
objArray[2] = new Estudiante("Cano", 27, "C", "Aguadilla");
objArray[3] = new Estudiante("Eribelto", 22, "F", "Moca");
// does not print anything when executed.
infoEst("Joshua");
infoEst("Liliana");
// the if statement block executes on index 0 so
// it prints Eribelto, Moca 4 times.
infoEst("Eribelto");
// All of these print "Eribelto" which is last in the array
System.out.println(objArray[0].nombre);
System.out.println(objArray[1].nombre);
System.out.println(objArray[2].nombre);
System.out.println(objArray[3].nombre);
}
}