Upper Case Conversion Required

Discussion in 'Java' started by Richard F.L.R.Snashall, Sep 8, 2006.

  1. I wrote the code snip below to do some extra checking on string to
    double conversion. I did not expect the need to call toUpperCase.
    What was I not anticipating? It just didn't seem logical to me,
    given the number of tools out there that create their floating point
    numbers with a lower case "e".

    -------------------------

    private java.util.Locale myLocale =
    java.util.Locale.getDefault( );

    private java.text.NumberFormat DFormat =
    java.text.NumberFormat.getInstance( myLocale );
    private java.text.ParsePosition DPosition =
    new java.text.ParsePosition( 0 );

    public double DparseDouble( String S ) throws NumberFormatException
    {
    // Trim it and check for a blank string.
    // Upper case is needed to match the "E" in scientific notation.

    String T = S.trim( ).toUpperCase( );
    if( T.length( ) == 0 )
    {
    throw new NumberFormatException( );
    }

    // Otherwise, try to parse it.

    DPosition.setIndex( 0 );
    Number parsedNumber = DFormat.parse( T, DPosition );

    // Failure will occur if none or only a part of the string
    // is properly parsed.

    if( DPosition.getIndex( ) < T.length( ) )
    {
    throw new NumberFormatException( );
    }

    return parsedNumber.doubleValue( );
    }
     
    Richard F.L.R.Snashall, Sep 8, 2006
    #1
    1. Advertising

  2. Couldnt help but ask - Why go through all this to convert a string to a
    double? You can do the same by new Double(yourString).doubleValue() or
    Double.valueOf(yourString).doubleValue().

    Is this that you are trying to parse out a double from an alphanumeric
    string, or you need the double in a special format (like with a
    lowercase e) ?

    -cheers,
    Manish

    Richard F.L.R.Snashall wrote:
    > I wrote the code snip below to do some extra checking on string to
    > double conversion. I did not expect the need to call toUpperCase.
    > What was I not anticipating? It just didn't seem logical to me,
    > given the number of tools out there that create their floating point
    > numbers with a lower case "e".
    >
    > -------------------------
    >
    > private java.util.Locale myLocale =
    > java.util.Locale.getDefault( );
    >
    > private java.text.NumberFormat DFormat =
    > java.text.NumberFormat.getInstance( myLocale );
    > private java.text.ParsePosition DPosition =
    > new java.text.ParsePosition( 0 );
    >
    > public double DparseDouble( String S ) throws NumberFormatException
    > {
    > // Trim it and check for a blank string.
    > // Upper case is needed to match the "E" in scientific notation.
    >
    > String T = S.trim( ).toUpperCase( );
    > if( T.length( ) == 0 )
    > {
    > throw new NumberFormatException( );
    > }
    >
    > // Otherwise, try to parse it.
    >
    > DPosition.setIndex( 0 );
    > Number parsedNumber = DFormat.parse( T, DPosition );
    >
    > // Failure will occur if none or only a part of the string
    > // is properly parsed.
    >
    > if( DPosition.getIndex( ) < T.length( ) )
    > {
    > throw new NumberFormatException( );
    > }
    >
    > return parsedNumber.doubleValue( );
    > }
     
    Manish Pandit, Sep 9, 2006
    #2
    1. Advertising

  3. Manish Pandit wrote:
    > Couldnt help but ask - Why go through all this to convert a string to a
    > double? You can do the same by new Double(yourString).doubleValue() or
    > Double.valueOf(yourString).doubleValue().
    >
    > Is this that you are trying to parse out a double from an alphanumeric
    > string, or you need the double in a special format (like with a
    > lowercase e) ?
    >


    It is user input. I need to (for my own piece of mind) be sure that the
    user has input "a double, a whole double, and nothing but a double";-)

    Most of the internal routines didn't seem to fit the bill. Lower case
    "e" was just [to me] a user-friendly allowance.
     
    Richard F.L.R.Snashall, Sep 9, 2006
    #3
  4. Hi,

    You should be okay if the user enters a 'e' or an 'E'. For example,
    Double.valueOf("1.23456789e9").doubleValue() will return 1.23456789E9
    and so will Double.valueOf("1.23456789E9").doubleValue().

    Maybe I am missing something again..

    Also, just a tip regarding null-proofing the blank check for the string
    - you might want to check for null before trimming it - more like if (
    S !=null && S.trim().length() >0 ){ .... }

    -cheers,
    Manish

    Richard F.L.R.Snashall wrote:
    > Manish Pandit wrote:
    > > Couldnt help but ask - Why go through all this to convert a string to a
    > > double? You can do the same by new Double(yourString).doubleValue() or
    > > Double.valueOf(yourString).doubleValue().
    > >
    > > Is this that you are trying to parse out a double from an alphanumeric
    > > string, or you need the double in a special format (like with a
    > > lowercase e) ?
    > >

    >
    > It is user input. I need to (for my own piece of mind) be sure that the
    > user has input "a double, a whole double, and nothing but a double";-)
    >
    > Most of the internal routines didn't seem to fit the bill. Lower case
    > "e" was just [to me] a user-friendly allowance.
     
    Manish Pandit, Sep 9, 2006
    #4
  5. Manish Pandit wrote:
    > Hi,
    >
    > You should be okay if the user enters a 'e' or an 'E'. For example,
    > Double.valueOf("1.23456789e9").doubleValue() will return 1.23456789E9
    > and so will Double.valueOf("1.23456789E9").doubleValue().
    >
    > Maybe I am missing something again..
    >


    I was using Double.parseDouble. According to the documentation:

    "static double parseDouble(String s)
    Returns a new double initialized to the value represented by
    the specified String, as performed by the valueOf method of class Double."

    That failed to work properly when the user input "e".

    > Also, just a tip regarding null-proofing the blank check for the string
    > - you might want to check for null before trimming it - more like if (
    > S !=null && S.trim().length() >0 ){ .... }


    I'll keep that in mind.
     
    Richard F.L.R.Snashall, Sep 9, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    730
    J├╝rgen Exner
    Dec 7, 2004
  2. Janice

    lower case to upper case

    Janice, Dec 10, 2004, in forum: C Programming
    Replies:
    17
    Views:
    1,222
    Richard Bos
    Dec 14, 2004
  3. B Williams
    Replies:
    9
    Views:
    489
    Yannick Tremblay
    Dec 19, 2006
  4. penny
    Replies:
    28
    Views:
    3,054
    Charlton Wilbur
    Mar 10, 2008
  5. BlackHelicopter
    Replies:
    0
    Views:
    619
    BlackHelicopter
    Jan 31, 2013
Loading...

Share This Page