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 :
then i want to get it like a float from the sesison . Can u help me please
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"));
then i want to get it like a float from the sesison . Can u help me please
float f = Float.parseFloat(session.g etAttribut e("solde") );
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
you should not use parseFloat
you shoul rahte use methiod
..floatValue()
you shoul rahte use methiod
..floatValue()
Float.parseFloat(s) requires String argument
if your caase (Float) session.getAttribute("sold e") returns you Float value
to get float from it you use method floatVlaue
float f = ( (Float) session.getAttribute("sold e") ).floatValue()
if your caase (Float) session.getAttribute("sold
to get float from it you use method floatVlaue
float f = ( (Float) session.getAttribute("sold
ASKER
with this
float f = ( (Float) session.getAttribute("sold e") ).floatValue()
i have error 500 , null pointer exeption .
float f = ( (Float) session.getAttribute("sold
i have error 500 , null pointer exeption .
if your caase (Float) session.getAttribute("soldi think it return a Stringe") returns you Float value
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("sold
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>Sess ion expired</title></head>");
out.println("<body><h1>Ses sion expired. Start with the Review screen</h1><BR></body></ht ml>");
return;
}
I usually do such check:
HttpSession sess = req.getSession(false);
if(sess == null) {
out.println("<html><head><
out.println("<body><h1>Ses
return;
}
ASKER
i forgot this part
session.setAttribute("sold e",new Float(rs.getString("solde" )));
:)
thank you
session.setAttribute("sold
:)
thank you
Yes, pretty important part :)
objects will be unhappy; in all justice this should rather be split
ASKER
ah ok .
and when i want to do this :
String m = req.getParameter("solde");
and then convert m to float ?
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);
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
waht user could have enetered there
ASKER
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("sold e",new Float(rs.getString("solde" )));
f = ( (Float) session.getAttribute("sold e") ).floatValue();
i want to make :
if (f>m)
{
f-=m;
}
but it dont work
i have
String m = req.getParameter("montant"
float mont = Float.parseFloat(m);
and this
session.setAttribute("sold
f = ( (Float) session.getAttribute("sold
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 f1 = Float.parseFlaot(m);
and then you can compare
f and f1
if(f>f1) will be valide syntax
>>float f = Float.parseFloat(session.g etAttribut e("solde") );
Won't work. Session returns Object. You need
Won't work. Session returns Object. You need
float f = Float.valueOf((String)session.getAttribute("solde"));
I think this would be fair to split 250/250 between http:#35734130 and http:#35734174