Java if statements without brackets creates unexpected behaviour -
why 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 first tier of brackets, not work , instead fail logically separate if statements. i.e. never go beyond first if statement , won't work intended.
if(name.equals("email_stub")) if(emailstub == "") emailstub = results.getstring("text"); else if(name.equals("fax")) if(fax == "") fax = results.getstring("text");
thanks.thought weird when ran issue.
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 this:
if(name.equals("email_stub")) if(emailstub == "") emailstub = results.getstring("text"); else if(name.equals("fax")) if(fax == "") fax = results.getstring("text");
without curly brackets, else
reference first if
before it.
and @hovercraft commented:
avoid
if(emailstub == "")
, insteadif (emailstub.isempty())
orif ("".equals(emailstub))
since should never compare strings==
or!=
. consider use oftrim()
here, suchif (emailstub.trim().isempty())
.
Comments
Post a Comment