1

I have an If statement in Java but it doesn't funtion correctly. I put a condition but the if doesn't respect that.

My code :

btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String name="";
            String surname="";
            name = txtName.getText().toString();
            surname = txtSurname.getText().toString();
            if (name!="" && surname!="") {
                msgAlarm.setText("<html>Insert all data<br/>before continuing!<html>");
                 Runnable runnable =
                         (Runnable) Toolkit.getDefaultToolkit().getDesktopProperty("win.sound.exclamation");
                 runnable.run();
            }else {
            System.out.println("Else Started");
            }

However when I run the program and the txtName and txtSurname are empty, Java run first statement. How can I resolve?
Many Thanks.

Gabriel
  • 464
  • 4
  • 17
  • Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Apr 07 '18 at 12:25
  • To check if a String is empty, use the `.isEmpty()` method, sometimes called after calling `.trim()` to clear trailing and leading white space. Also no need to call `.toString()` on a method that *returns* a String – Hovercraft Full Of Eels Apr 07 '18 at 12:26
  • 1
    Clearly, Java's `if` [isn't broken](https://pragprog.com/the-pragmatic-programmer/extracts/tips). Two problems: 1. [That's not how you compare strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). 2. My guess is you don't mean "and," you mean **or** (`||`). Your current code only goes into the body of the `if` when `name` **and** `surname` are **both** `""`. – T.J. Crowder Apr 07 '18 at 12:27
  • @HovercraftFullOfEels Thanks, it function. What do you mean with "checks if the two object references are the same"? – Gabriel Apr 07 '18 at 12:31
  • 1
    @T.J.Crowder Thanks, I corrected the code and it function! – Gabriel Apr 07 '18 at 12:31
  • You had better look up the terms, since they are key to understanding Java's implementation of object-oriented programming. – Hovercraft Full Of Eels Apr 07 '18 at 12:32
  • @HovercraftFullOfEels Thank you very much, I'll remember that – Gabriel Apr 07 '18 at 12:34
  • @HovercraftFullOfEels If you want you can write your comment as an answer and I close this question – Gabriel Apr 07 '18 at 12:36
  • No one can write an answer since the question has been closed as a duplicate. It has been asked thousands of times on this site, and we really don't need one more of the same. In the future, please [search the site for similar questions](https://www.google.com/search?&q=site%3Ahttps%3A%2F%2Fstackoverflow.com%2F+java+if+statement+checking+strings+doesn%27t+work) before asking. . – Hovercraft Full Of Eels Apr 07 '18 at 12:39

0 Answers0