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 == "") , instead if (emailstub.isempty()) or if ("".equals(emailstub)) since should never compare strings == or !=. consider use of trim() here, such if (emailstub.trim().isempty()).

see how compare strings in java?.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -