-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,915
  • 9
  • 50
  • 89

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
  • 8,079
  • 2
  • 26
  • 35
  • @user3511983 You are very welcome (and welcome to SO as well ;) ). You can mark this answer as _accepted_ if it satisfied you. – ifloop Apr 08 '14 at 17:11