tags:

views:

93

answers:

3

The problem with this code is I'm getting the popularity of an author as 0% (I mean zero percent if the number of borrowed books is 14 and the total number of books borrowed of the selected author is 3 - it should be 21.42%). Why is this happening?

All result are correct except the last one:

Author is 0 % popular (for above given data)

<%
String requestedoprations = request.getParameter("popularity");
if("check".equalsIgnoreCase(requestedoprations)){
    int num=LimsHandler.getInstance().popularitycheck(
        request.getParameter("selectedauthor"));
    if(num!=0){
        Limsdetails[] list = LimsHandler.getInstance().libsdetails();
        String totbks=list[0].getTot_books();
        String totbrwdbk=list[0].getTot_borrowed_bks();
        int totbksint=Integer.parseInt(totbks);
        int totbrwdbksint=Integer.parseInt(totbrwdbk);
        float per=(num/totbrwdbksint)*100;          
%>
<font color="brown">
    <b>Total No of Books Available in Library is : <%=totbksint %><br></br>
    Out of which <%=totbrwdbksint %> are borrowed.<br></br>
    <b>No of readers reading Author 
        <%=request.getParameter("selectedauthor") %>'s book. : 
        <%=num %></b><br></br>
    <b> Author <%=request.getParameter("selectedauthor") %> is <%=per %> % 
        popular!</b><br></br>
</font>

<%}else{ %>
    <h4 align="center">
        <font color="red">
            <img border="0" src="images/close.PNG" ><br></br>
            Oops! some error occurred!
        </font>
    </h4>
<%
}
out.flush();
%>

<%} %>
+5  A: 

This isn't really a JSP problem - it's how Java deals with integer arithmetic. The relevant lines are:

int num = LimsHandler.getInstance().popularitycheck(...);
int totbrwdbksint = Integer.parseInt(totbrwdbk);
float per = (num / totbrwdbksint) * 100;

You're performing an "int / int" division and then multiplying by 100. That will perform the division using integer arithmetic - so the result will be 0. Multiplying 0 by 100 still gives 0.

The easiest way to fix it is to make one of the values a float or double. For example:

int num = LimsHandler.getInstance().popularitycheck(...);    
float totbrwdbksint = Integer.parseInt(totbrwdbk);
float per = (num / totbrwdbksint) * 100;

Alternatively, you could cast within the expression:

int num = LimsHandler.getInstance().popularitycheck(...);
int totbrwdbksint = Integer.parseInt(totbrwdbk);
float per = (num / (float) totbrwdbksint) * 100;

At this point the division will be performed using floating point arithmetic, and you'll get the answer you expect.

Jon Skeet
I personally would also prefer (100*num) / totbrwdbksint. That can up the accuracy a bit if num is significantly smaller than totbrwdbksint.
extraneon
great it worked.thanx a lot.can u tell me one more thing.the result is something like 26.323232 % .i want it to round upto 2 decimaol places .how to do this?
Robin Agrahari
@extraneon: On the other hand it leads to potential overflow if `num` is very large :)
Jon Skeet
A: 

The way your calcualtion

float per=(num/totbrwdbksint)*100  

is performed is rounding the result of (num/totbrwdbksint) to zero.

Try

float per=((float)num/(float)totbrwdbksint)*100  

to get a better result.

Steve De Caux
+1  A: 

It's not a solution to your original question, but I would recommend learning two new things:

  1. JSTL
  2. CSS

You should have neither scriptlets nor styling embedded in your JSPs this way. You'll thank yourself for making the effort some day, because maintaining and restyling your pages will be much easier.

duffymo