?
Solved

convert string to float

Posted on 2011-05-10
24
Medium Priority
?
581 Views
Last Modified: 2012-05-11
hello ,
i want to get float from session .
this is my code .

i put the string in the session here :
session.setAttribute("solde",rs.getString("solde"));

Open in new window


then i want to get it like a float from the sesison . Can u help me please
0
Comment
Question by:keviin555
  • 13
  • 5
  • 2
  • +1
21 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35734120
float f = Float.parseFloat(session.getAttribute("solde"));
0
 
LVL 47

Accepted Solution

by:
for_yan earned 1000 total points
ID: 35734130

Perhaps you ean something like that:


<%@ page language="java" import="java.util.*" %>
<html>
<body>
Session attributes:
<%
  session.setAttribute("test.name", "Test Attribute List");
  session.setAttribute("test.float", new Float(5.0));
  session.setAttribute("test.int", new Integer(10));
  session.setAttribute("test.Object", new StringBuffer("StringBuffer"));
  session.setAttribute("test.boolean", new Boolean(true));
  session.setAttribute("test.double", new Double(343.1));
  for (Enumeration e = session.getAttributeNames(); e.hasMoreElements(); ) {     
    String attribName = (String) e.nextElement();
    Object attribValue = session.getAttribute(attribName);
%>
<BR><%= attribName %> - <%= attribValue %>

<%
}
%>
</body>
</html>

Open in new window

from
http://www.xyzws.com/JSPfaq/how-to-display-all-the-session-variables-in-an-html-page/33

0
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 35734174
you could also put the float directly in the session


session.setAttribute("solde",new Float(rs.getString("solde")));

then access it using:

float float = ((Float) session.getAttribute("solde")).floatValue();
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:keviin555
ID: 35734177
float f = Float.parseFloat(session.getAttribute("solde"));

it don't work :/

 m
@for_yan

i dont understand :/ and  i dont use jsp , i use servlet .
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35734193
you should not use parseFloat
you shoul rahte use methiod

..floatValue()
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35734203
Float.parseFloat(s) requires String argument

if your caase  (Float) session.getAttribute("solde") returns you Float value
to get float from it you use method floatVlaue

float f = ( (Float) session.getAttribute("solde") ).floatValue()
0
 

Author Comment

by:keviin555
ID: 35734253
with this
float f = ( (Float) session.getAttribute("solde") ).floatValue()
i have error 500 , null pointer exeption .

if your caase  (Float) session.getAttribute("solde") returns you Float value
i think it return a String
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35734275

Did it retrivee anything elase form this session?
Myabe it didn't retriev session? Id session is null,
Then session.getAtribute whill give null pointer excpetion

Are you sure this part
session.setAttribute("solde",new Float(rs.getString("solde")));
worked?

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35734295
Maybe session could have timed out, if you waited too long?

I usually do such check:

 HttpSession sess = req.getSession(false);
    if(sess == null) {
   out.println("<html><head><title>Session expired</title></head>");
    out.println("<body><h1>Session expired. Start with the Review screen</h1><BR></body></html>");
    return;
     }
0
 

Author Comment

by:keviin555
ID: 35734333
i forgot this part
session.setAttribute("solde",new Float(rs.getString("solde")));

:)

thank you
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35734338
Yes, pretty important part :)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35734342
objects will be unhappy; in all justice this should rather be split
0
 

Author Comment

by:keviin555
ID: 35734345
ah ok .

and when i want to do this :

String m = req.getParameter("solde");

and then convert m to float ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35734351

No, req.getParameter(..) returns String
after that you can use that

float f = Float.parseFloat(m);

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35734361
However the good practice is ffirst to check if this string m
can be converted to float

Say,

boolean goodNumber = true;

try{

new Float(m);

}
catch {
goodNumber = false;
}

if(!goodNumber) {out.prinltn("error in input");
//and end here because you cannot go further
}

float f = Float.parseFloat(m);



0
 
LVL 47

Expert Comment

by:for_yan
ID: 35734363
Becuase often thsi string comes from user's input in the form, and who knows
waht user could have enetered there
0
 

Author Comment

by:keviin555
ID: 35734563
ok thank you very much . But i have a problem with the firs float . There is a difference between (Float and float ) ?

i have
String m = req.getParameter("montant");
float mont = Float.parseFloat(m);  

and this
session.setAttribute("solde",new Float(rs.getString("solde")));
f = ( (Float) session.getAttribute("solde") ).floatValue();


i want to make :

if (f>m)
{
f-=m;
}

but it dont work
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35734621

float - is primitive reprsenting float number

Float - is object - wrapper around the float primitive
Float is not the same as float, you cannot add two Float objects,
you cannot multiply them, etc
float number is just a field of the Float object.
therefore sya floatValue() is a method of Object Float which exposes
this primitive field; new Float(float f) creates object with the primitive field eqqual to f.
Float.parseFloat(String s)  - static method of the Float class - kind of convenience
method which goes into details nanlyses all characters of string and figures out
if these characters make up something that may reoresent float primitive number
and returns this number, or, alternaticvely, throws exception indicating that this String is no
good for making up a float number
Static method has nothing to do with any particular Float isnstance

method req.getParameter("name")
always returns String, that String may either represent a number, or a text or anything,
but at the moment when you get it
m = req.getParameter("name"), m is just a String

Therefore if(f>m)  makes no sense, f is float, m is String
What you can do is to analyze what kind of Struing is m (see my previous post)
and maybe ity is inded can be converted to number
 



 
 




0
 
LVL 47

Expert Comment

by:for_yan
ID: 35734632
then you can make

float f1 = Float.parseFlaot(m);
and then you can compare
  f and f1
if(f>f1) will be valide syntax
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35739105
>>float f = Float.parseFloat(session.getAttribute("solde"));

Won't work. Session returns Object. You need
float f = Float.valueOf((String)session.getAttribute("solde"));

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35743030
I think this would be fair to split 250/250 between http:#35734130 and http:#35734174
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

807 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