Does not enter the if statement, even if the data matches [duplicated]

2

In the following fragment of code, I hope that the text is printed inside the if but when I execute it I see that it does not enter. I have verified that both objects have the same data to be considered equal. What can I be doing wrong?

public void maestrosColonia(Profesor [] profesores,String colonia){
    for(int i=0;i<profesores.length;i++) {
        if(colonia == profesores[i].getDireccion().getColonia()){
            System.out.println(profesores[i].getNombre()+" "+profesores[i].getNss()+" "+profesores[i].getTelefonos().getTelCasa());
        }
    }
}
    
asked by Lira 31.03.2016 в 16:28
source

1 answer

3

Use equals instead of ==

if(colonia.equals(profesores[i].getDireccion().getColonia())){

Or if it's a better String use equalsIgnoreCase

if(colonia.equalsIgnoreCase(profesores[i].getDireccion().getColonia())){
    
answered by 31.03.2016 / 16:45
source