JAVA - Problem with the IF ELSE conditional when using the value of a STRING

1

The code is very simple. I just have to get the type of 'storage' that an article has. This can be from the 'deposit' or from the 'shelves / gondolas'. When obtaining that registry, I proceed to carry out a different process of each one. The problem is that the conditional does not work for me, regardless of the value I receive.

String tipo = dao_articulos.obtener_tipo(art.getLocalizacion());

if (tipo == "DEPÓSITO") {

} 

if (tipo == "ESTANTE") {

} 

I have corroborated with the System.out.print() the value that receives 'type', and without problems, it receives the correct values.

Even if the type is 'DEPOSIT' or 'SHELF', the code 'jumps' the conditional. I know something is wrong but I do not detect it. Sometimes we get so complicated about such simple things.

    
asked by Adolfi Téllez 15.12.2018 в 16:09
source

1 answer

1

Hi, you could use string comparison with equals in this way

   String tipo = dao_articulos.obtener_tipo(art.getLocalizacion());
    if(tipo.equals("DEPÓSITO")){
     System.out.println("deposito");
    }else if(tipo.equals("ESTANTE")){
      System.out.println("estante");
    }else{
   System.out.println("no es deposito ni estantes es "+tipo);
    }

and to ignore uppercase or lowercase you can use equalsIgnoreCase:

String tipo = "DePoSITO";
if(tipo.equalsIgnoreCase("DEPOSITO")){
 System.out.println("deposito");
}

It should be noted that the accents and / or uppercase and lowercase are important. It should work unless the error is another.

    
answered by 16.12.2018 / 01:55
source