-1

Hey im having trouble with checking if a JTextField value is given. I think I have the correct code, but it is not saying anything that could possible be broken.

Q: Why wont it display or run the Game.main(null) when the value of the textfield is "Dianamu" ?

public void mouseClicked(MouseEvent e) {
   String values = textField1.getText();
   if(values == "Dianamu"){
       Game.main(null);
   }
   System.out.println("Login Works:");
}

Thanks in Advance

Dianamu
  • 121
  • 1
  • 3
  • 12

1 Answers1

1

if(values.equals("Dianamu")){ Game.main(null); }

EXPLANATION: == compares object references; .equals() compares values

Leo
  • 2,097
  • 21
  • 35
  • 2
    Dumping code on an unexpecting OP isn't as helpful as it might seem (although I tend to do as well), take the time to explain why your approach should be used and why the OP's approach didn't work... – MadProgrammer Jul 27 '13 at 00:53
  • == compares object references; .equals() compares values – Leo Jul 27 '13 at 00:53
  • Don't tell me, tell the OP ;) – MadProgrammer Jul 27 '13 at 00:56
  • 1
    Good, now include it to your answer. To do that use [edit] option. Also format your code sample. You can do it with `{}` button from editor's menu. – Pshemo Jul 27 '13 at 00:57
  • How could this work with doing it with a JPasswordField? Would I need to de-code the password (Im just using local string variables for now to test) – Dianamu Jul 27 '13 at 00:58
  • about JPasswordField comparison: http://stackoverflow.com/questions/13995986/compare-two-jpasswordfields-in-java-before-saving – Leo Jul 27 '13 at 01:00
  • @Heloic `field.getPassword()` will return array of characters. To create String that will represent content of JPasswordField you can use `new String(field.getPassword())` then you can use `equals` on this String and compare it with your other Strings like `new String(field.getPassword()).equals("secret")`. – Pshemo Jul 27 '13 at 01:03