-1

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;
}
Frakcool
  • 10,540
  • 9
  • 46
  • 78

1 Answers1

2

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.

Community
  • 1
  • 1
ifloop
  • 7,489
  • 2
  • 24
  • 35