You are comparing two String
objects via !=
. But Strings have to be compared by .equals
:
if (!ad.equals("")) {
//foo
}
See How do I compare Strings in Java for further information.
Domanda
I am trying to code a method where i can choose to change the value of the variable "address", and if i do not want to change, i can have the option not to. I'm not sure what is the problem here, thank you very much for the help.
public void change(){
keyboard t = new keyboard();
String ad;
System.out.println("If you don't wish to change, just press enter");
ad = t.readString("Type in the new address: ");
if (ad != "")
adress = ad;
}
Soluzione
You are comparing two String
objects via !=
. But Strings have to be compared by .equals
:
if (!ad.equals("")) {
//foo
}
See How do I compare Strings in Java for further information.