Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4096613

Float and Double.valueOf(String) don't throw NumberFormatException.

    XMLWordPrintable

Details

    • x86, sparc
    • solaris_2.5, windows_2000

    Description

      Name: aaC67449 Date: 12/03/97



      Float.valueOf(String),
      Double.valueOf(String),
      Float(String) and
      Double(String)
      accept integer literals (i.e. literals without decimal point).
      According to the JLS (20.9.17, 20.10.16), the NumberFormatException
      shold be thrown.

      JLS 20.9.17:
      "
      20.9.17 public static Float valueOf(String s)
      throws NullPointerException, NumberFormatException

      The string s is interpreted as the representation of a floating-point value and a Float object representing that value is created and returned.

      If s is null, then a NullPointerException is thrown.

      Leading and trailing whitespace (?20.5.19) characters in s are ignored. The rest of s should constitute a FloatValue as described by the lexical syntax rules:

          FloatValue:

                  [Sign] Digits . [Digits] [ExponentPart]

                  [Sign] . Digits [ExponentPart]

      where Sign, Digits, and ExponentPart are as defined in ?3.10.2. If it does not have the form of a FloatValue, then a NumberFormatException is thrown. Otherwise, it is regarded as representing an exact decimal value in the usual "computerized scientific notation"; this exact decimal value is then conceptually converted to an "infinitely precise" binary value that is then rounded to type float by the usual round-to-nearest rule of IEEE 754 floating-point arithmetic. Finally, a new object of class Float is created to represent this float value.

      Note that neither F nor f is permitted to appear in s as a type indicator, as would be permitted in Java source code (?3.10.1).

      "
      -------------Example ---------------

      public class Test {

      public static void main( String argv[] ) {
       try {
           Float.valueOf("1");
           System.out.println("FAILED. No NumberFormatException.");
       }catch(NumberFormatException e) {
          System.out.println("PASSED");
       }

       try {
           Float.valueOf("1234");
           System.out.println("FAILED. No NumberFormatException.");
       }catch(NumberFormatException e) {
          System.out.println("PASSED");
      }
       
      }

      ---------Output from the test---------------------
      FAILED. No NumberFormatException.
      FAILED. No NumberFormatException.
      --------------------------------------------------



      ======================================================================

      Name: rmT116609 Date: 09/19/2002


      DESCRIPTION OF THE PROBLEM :
      Long.valueOf(" 12 "); throws NumberFormatException, Same with Integer

       

      Double.valueOf(" 12 "); does a trim and doesn't throw anything. Same as Float.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Consistency, either trim the string in the valueOf method all-the-time or don't trim.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class NumberTest
      {

         public static void main(String[] args)
         {

            String numberString = " 20 ";
            try
            {
               System.out.println(Long.valueOf(numberString));
            }
            catch (NumberFormatException npe)
            {
               System.out.println("caught number format expection");
            }
            try
            {
                System.out.println(Double.valueOf(numberString));
            }
            catch (NumberFormatException npe)
            {
               System.out.println("caught number format expection");
            }

         }
      }
      ---------- END SOURCE ----------
      (Review ID: 164754)
      ======================================================================

      Attachments

        Issue Links

          Activity

            People

              darcy Joe Darcy
              aalievsunw Artem Aliev (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: