-4

Why does this work:

if(name.equals("email_stub")) {
    if(emailStub == "")
        emailStub = results.getString("text");
} else if(name.equals("fax")) {
    if(fax == "")
        fax = results.getString("text");
} 

But without the first tier of brackets, it will not work and instead fail to logically separate the if statements. i.e. it will never go beyond the first if statement and won't work as intended.

if(name.equals("email_stub"))
    if(emailStub == "")
        emailStub = results.getString("text");
else if(name.equals("fax"))
    if(fax == "")
        fax = results.getString("text");

I thought it weird when I ran into this issue.

insidesin
  • 735
  • 2
  • 8
  • 26
  • 2
    How is the `else` supposed to guess it's related to the first `if` and not the second one ? The compiler doesn't look at indentation. – Denys Séguret Jul 23 '15 at 15:04
  • 2
    Always use curly braces to enclose any and all control blocks, such as if and else blocks, especially as a new programmer. Else you're liable to repeatedly shoot yourself in the foot, as you're doing now. – Hovercraft Full Of Eels Jul 23 '15 at 15:05
  • I'm far from a new programmer. I also disagree that curly brackets are required on one action line if statements. Just wanted to know what was going on here. – insidesin Jul 23 '15 at 15:07
  • It's almost like people use braces for a reason other than just decoration. – khelwood Jul 23 '15 at 15:08
  • If you had used braces fully, you would not have mis-indented the else block in the second block of code. But again, feel free to interpret this as you wish. – Hovercraft Full Of Eels Jul 23 '15 at 15:39
  • @HovercraftFullOfEels Strange. My posts were deleted. I must have upset someone. – insidesin Jul 24 '15 at 15:52
  • @insidesin: not me, but it means someone flagged your comments and the moderator removed them, and they will do that, even if the sin is only being too chatty. It happens from time to time, no biggie. But again, your comments didn't bother me, as we're both big boys (assuming you're male that is). – Hovercraft Full Of Eels Jul 24 '15 at 16:14
  • @HovercraftFullOfEels common occurrence on the internet today, give people a sense of power and bias and they'll use it disastrously poorly. Until you actually start paying them they won't escalate beyond anything other than a willed pressure. : – insidesin Jul 24 '15 at 16:25
  • I'm not sure I'd go as far as to say "disastrously" just yet. It's not like deleting any of our posts is going to have a monumental effect on anything. – Hovercraft Full Of Eels Jul 24 '15 at 16:27
  • @HovercraftFullOfEels In the context of their one real job 'moderation', it's quite disastrous. – insidesin Jul 25 '15 at 08:31

1 Answers1

7

Because this:

if(name.equals("email_stub"))
    if(emailStub == "")
        emailStub = results.getString("text");
else if(name.equals("fax"))
    if(fax == "")
        fax = results.getString("text");

Is actually this:

if(name.equals("email_stub"))
    if(emailStub == "")
        emailStub = results.getString("text");
    else if(name.equals("fax"))
        if(fax == "")
            fax = results.getString("text");

Without the curly brackets, the else will reference the first if before it.


And as @Hovercraft commented:

Avoid if(emailStub == "") and instead do if (emailStub.isEmpty()) or if ("".equals(emailStub)) since you should never compare Strings with == or !=. Also consider use of trim() here, such as if (emailStub.trim().isEmpty()).

See How do I compare strings in Java?.

Community
  • 1
  • 1
Fred Porciúncula
  • 8,533
  • 3
  • 40
  • 57
  • 3
    One plus, **but**, avoid `if(emailStub == "")` and instead do `if (emailStub.isEmpty())` or `if ("".equals(emailStub))` since you should never compare Strings with `==` or `!=`. Also consider use of `trim()` here, such as `if (emailStub.trim().isEmpty())` – Hovercraft Full Of Eels Jul 23 '15 at 15:06
  • Yes I agree @HovercraftFullOfEels that's one of my 'why isn't this working' tests, left it in there on accident ahaha whoops. – insidesin Jul 23 '15 at 15:14
  • @ThiagoPorciúncula Without the quotes? – Chetan Kinger Jul 23 '15 at 15:37