-
problems with Float.parseFloat()
Hi,
I am writing a pgm in which I need to calculate the total proce of all
the products selected by the user on the GUI.but then I am using IBM JDK
1.1.7 ,,,so I am getting an error with
Float.parseFloat() method, I think the method is not existing in the jdk1.1.7
API, but then I dont know how to slove the problem, I am giving the code
below for the program, could anyone have a look at it and solve my problem....
Satish
-----------------------------------------------------------------
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class cart extends HttpServlet
{
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
public void init(ServletConfig sc) throws ServletException
{
try
{
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
con = DriverManager.getConnection("jdbc:db2:sample","webs",
"websdba");
super.init(sc);
}
catch(Exception e) {e.getMessage() ; }
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String TotalPrice = new String();
res.setContentType("text/html");
PrintWriter out = res.getWriter();
HttpSession session = req.getSession(false);
Object[] items = (Object []) session.getValue("cart.items");
out.println("<html> ");
out.println("<body bgcolor = white >");
out.println("<H3>you currently have the following items in your cart:</H3><br>");
out.println("<form> ");
out.println("<table>");
if( items != null )
{
for (int i = 0 ; i < items.length; i++ )
{
try
{
String str1 = (String) items[ i ] ;
pst = con.prepareStatement("select PRICE from ITEMTAB where
ITMNAME = ? " );
pst.setString(1,str1);
rs = pst.executeQuery();
while(rs.next())
{
String curprice = rs.getString("price");
out.println("<tr>");
out.println("<td> ");
out.println( "The Price of " + "
" + str1 + " is " + " " + curprice );
out.println("</td> ");
out.println("</tr>");
out.println("</table>");
out.println("<br>");
String totalval = "0.0" ;
session.putValue("total.cost", (new Float(Float.parseFloat(totalval)
+ Float.parseFloat(curprice)).toString()) );
}
}
catch(Exception e) { e.getMessage(); }
}
out.println("<table border = 1 cellpadding = 2 cellspacing = 3>");
out.println("<tr>");
out.println("<td>");
String totval = (String)session.getValue("total.cost");
out.println("The Total Cost of all the Products in the Cart is : " +
totval );
out.println("</td>");
out.println("</tr>");
out.println("</table>");
out.println("</form>");
}
else
{
out.println("There are no items in the session value for cart.items"
);
}
out.println("</body>");
out.println("</html> ");
}
}
-
Re: problems with Float.parseFloat()
The pre-Java-2 version of this is "Float.valueOf(totalval)", which converts
the String to a Float. If you want a float instead of a Float, use
"Float.valueOf(totalval).floatValue()".
But allow me to suggest an simpler method:
float curprice = ...;
float totalval = 0.0;
session.putValue("total.cost", new Float(totalval + curprice));
The second parameter of putValue() takes an Object. You aren't limited to
using a String there.
satish <satish_141@yahoo.com> wrote in message
news:39bcb1ce$1@news.devx.com...
>
>
>
> Hi,
>
> I am writing a pgm in which I need to calculate the total proce of all
> the products selected by the user on the GUI.but then I am using IBM JDK
> 1.1.7 ,,,so I am getting an error with
>
> Float.parseFloat() method, I think the method is not existing in the
jdk1.1.7
> API, but then I dont know how to slove the problem, I am giving the code
> below for the program, could anyone have a look at it and solve my
problem....
>
> Satish
>
> -----------------------------------------------------------------
> String totalval = "0.0" ;
> session.putValue("total.cost", (new
Float(Float.parseFloat(totalval)
> + Float.parseFloat(curprice)).toString()) );
>
-
Re: problems with Float.parseFloat()
Thanks for ur answer Paul
satish
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|