Solved

jsp true and false better way

Posted on 2002-07-22
5
447 Views
Last Modified: 2006-11-17
I am using the following code a number of times on a jsp page.  Is there a more efficient way to do this

and futher down I use it the same way
minus  boolean  display = false ;
------------------------------
<%
boolean  display = false ;
if ( (new Integer(passedCalculatedElecSerDay)).intValue()>0 )
{
display = true;
}
if ( (new Integer(passedCalculatedElecSerYear)).intValue()>0 )
{
display = true;
}
if  ( display == true  )
{
%>    
do this
<%
 } else {
%>
do that
<% } %>
0
Comment
Question by:schroom
  • 2
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 200 total points
ID: 7169756
 Why do you use the: new Integer(passedCalculatedElecSerYear)).intValue()>0 twice?. Can you not do it only once or the "passedCalculatedElecSerYear" chanegs all the time? And the: if  ( display == true  ) could be written as if  ( display ).

  Besides that I can't see any other way you could make your source code better.

  Hope it helps.
0
 

Author Comment

by:schroom
ID: 7169841
okay - but maybe creating a formMode would work
0
 
LVL 2

Expert Comment

by:flumpman
ID: 7169849
In addition, you should use

int Integer.parseInt(String) instead of new Integer(String).intValue() as this is more efficient.  It avoids the creation of an object that has to be garbage collected.

--
flumpman
0
 

Author Comment

by:schroom
ID: 7169864
flumpman thanks for more that extra tip,
I will change my code
0
 
LVL 35

Expert Comment

by:girionis
ID: 7169898
 Thank you for the points schroom ]:-). I am glad I helped. What exactly is the formMode?

  As for the Integer.parseInt(String) I think this is a betetr solution as well. It would be wise to catch a NumberFormatException as well:

int passedCalculatedElecSerYear = 0; // Default value
try
{
    passedCalculatedElecSerYear = Integer.parseInt(<string>);
}
catch (NumberFormatException nfe) {}

  Hope it helps.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 84
split53 challenge 7 95
maven project error 5 54
servlet requestdispatcher include and forward differences 1 30
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question