I intend to take for scanner
a "yes" or "no" , and if the comparison grat == "si"
is true, I should take as true
. But it does not work.
System.out.println("¿Envio gratuito? (si no)");
String grat = scan.nextLine();
boolean gratuito = (grat == "si");
System.out.println(gratuito);
In java, the comparison of
Strings
must not be done by==
you must use the equals() method :
In fact, it seems to be a common error when we start programming in some languages, in the case of Java, I suggest you review:
How to properly compare Strings (and objects) in Java?
Partner, you have a very simple error, remember that for the comparison of strings (String) we use
equals
, you are using==
it as if it were an integer.