Link to home
Start Free TrialLog in
Avatar of keviin555
keviin555

asked on

convert string to float

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
Avatar of Mick Barry
Mick Barry
Flag of Australia image

float f = Float.parseFloat(session.getAttribute("solde"));
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of keviin555
keviin555

ASKER

float f = Float.parseFloat(session.getAttribute("solde"));

it don't work :/

 User generated image
@for_yan

i dont understand :/ and  i dont use jsp , i use servlet .
you should not use parseFloat
you shoul rahte use methiod

..floatValue()
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()
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

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?

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;
     }
i forgot this part
session.setAttribute("solde",new Float(rs.getString("solde")));

:)

thank you
Yes, pretty important part :)
objects will be unhappy; in all justice this should rather be split
ah ok .

and when i want to do this :

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

and then convert m to float ?

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

float f = Float.parseFloat(m);

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);



Becuase often thsi string comes from user's input in the form, and who knows
waht user could have enetered there
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

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
 



 
 




then you can make

float f1 = Float.parseFlaot(m);
and then you can compare
  f and f1
if(f>f1) will be valide syntax
>>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

I think this would be fair to split 250/250 between http:#35734130 and http:#35734174