12 May 11 16 - compare float

Posted on 2011-05-10
Last Modified: 2012-05-11

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)

but it dont work
Question by:keviin555
    LVL 23

    Accepted Solution

    it should be

    if(f > mont){
    f -= mont

    Open in new window

    LVL 47

    Expert Comment

    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



    Author Comment

    ohhh i'm so stupid :/
    i thout that Float and float are dirrerent
    LVL 47

    Expert Comment

    Float and float are different!!!
    Float - object
    float - primitive number
    LVL 92

    Expert Comment

    What was wrong with what cmalakar posted?
    Looks like exactly what was needed.

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

    thats exactly what cmalakar had already suggested
    LVL 23

    Expert Comment

    >>What was wrong with what cmalakar posted?

    I agree ;-)

    And the comment from author "ohhh i'm so stupid :/" was at same time, as accepted solution's comment.
    I wonder how it was selected.
    LVL 23

    Expert Comment

    Yes, my first comment was completely ignored, which has the solution for the question, and it suggests the same as the accepted solutions comment.

    "objects" thanks for asking the explanation.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    tenRun challenge 28 72
    commonTwo  challenge 63 69
    maven java path setting 5 33
    wordappend challenge 8 54
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now