Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Please help with this java method

    I'm trying to return a true statement if the string represents a double. Here is what I compiled, please let me know if this is right. Thank you

    [code]
    public static boolean isDouble(String source) {
    if (string == null) {
    return false;
    }
    int length = string.length();
    if (length == 0) {
    return false;
    }
    int i = 0;
    if (string.charAt(0) == '-') {
    if (length == 1) {
    return false;
    }
    i = 1;
    }
    for (; i < length; i++) {
    char c = string.charAt(i);
    if (c < '0' || c > '9') {
    return false;
    }
    }
    return true;

    } //End isDouble
    [code]

  • #2
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    A-Silent-Ninjah, I guess you came from c# or java world. And javascript is not java. Javascript is weak-typed and prototype-oriented language (instead java). So this will not work here. I suspect you are novice in javascript and you dont know that many things here are made the more or less differently. I will give some hints but in the future recommend you to find good books and learn more.

    Code:
    isFinite(value)
    isNaN(value)
    parseFloat(value)
    parseInt(value)
    Number(value)
    I prefer to use Number() function because it returns the more correct result accordingly the following table of the type conversation:
    Code:
    Number(undefined) == NaN
    Number(null) == 0
    Boolean(false) == 0
    Boolean(true) == 1
    Number(number) == number
    Number(object) == the default value of the object
    Number(string) == number if the string presents the valid numeric value otherwise NaN

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,118
    Thanks
    80
    Thanked 4,555 Times in 4,519 Posts
    Siberia is right that this is a terrible way to check for double, even in Java coding!

    But it *CAN* work by just simplifying it:
    Code:
    function isDouble(source) 
    {
        if (string == null) return false;
        var length = string.length();
        if (length == 0) return false;
        var i = 0;
        if (string.charAt(0) == "-" ) 
        {
            if ( length == 1) return false;
            i = 1;
        }
        for (; i < length; i++) 
        {
            var c = string.charAt(i);
            if (c < '0' || c > "9") return false;
        }
        return true;
    } //End isDouble
    Notice that JS allows either '...' or "..." for strings *AND* there is not distinction between characters and strings.

    EVEN IN JAVA, you would be *MUCH* better off using a regular expression, instead of that hodgepodge.

    In JavaScript, you could do it thus:
    Code:
    function isDouble( source )
    {
        if ( source == null ) return false;
        var renum = /^\-?\d+(\.\d*)$/
        return renum.test( source );
    }
    You can do it almost the same as that in Java.

    BUT...

    As Siberia said, you should learn to use the library functions that are built into whatever language you choose!
    Code:
    function isDouble( source )
    {
        if ( source == null ) return false;
        return ! isNaN( Number(source) );
    }
    or, if you want to exclude boolean values and the like from your test, then just use parseFloat instead:
    Code:
    function isDouble( source )
    {
        if ( source == null ) return false;
        return ! isNaN( parseFloat(source) );
    }
    And, finally, even more compactly:
    Code:
    function isDouble( source )
    {
        return (source != null ) && ( ! isNaN( parseFloat(source) ) );
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Old Pedant, try this
    Code:
    isDouble('1e2') // true because '1e2' => 100
    isDouble('1w2') // true because '1' => 1
    That is why I prefer to use the Number() fucntion. Number() tries to convert an argument to a valid numeric value and returns it as the result or returns NAN otherwise.

    better off using a regular expression
    This is the exellent regex validating numeric strings (-:
    Code:
    /^[+-]?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?$/
    Last edited by siberia-man; 02-17-2011 at 11:52 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •