开发者

Problem regarding calculation in JSP

开发者 https://www.devze.com 2022-12-16 01:42 出处:网络
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

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 totbksi开发者_C百科nt=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();
%>

<%} %>


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消