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

Inconsistencies in parseXXX() Parameter Handling

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 1.4.2
    • core-libs
    • x86
    • windows_2000



      Name: rmT116609 Date: 02/12/2004


      FULL PRODUCT VERSION :
      java version "1.4.2_03"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_03-b02)
      Java HotSpot(TM) Client VM (build 1.4.2_03-b02, mixed mode)

      java version "1.5.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32c)
      Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      Each numeric wrapper class has a parseXXX() method. The parseFloat() and parseDouble() methods automatically trim their string parameters before attempting to convert them whereas the methods, parseByte(), parseShort(), parseInt() and parseLong() do not. Instead of trimming their string parameters, these methods throw a NumberFormatException.

      Also, please note that the Boolean.valueOf() method does not trim its string paramter either. When you pass the string, "true\n", to this method it returns false.

      I think there needs to be consistency. Either all parseXXX() methods should trim their string parameters before the attempted conversion or they should all throw NFE when presented with leading/trailing whitespace in their parameters.

      I have a test case below to illustrate the problem.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      see the test case.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      class Tester {
        public static void main(String[] args) throws Exception {
          String boolString = "true\n";
          String testString = " 4\n";

          // First we try boolean
          Boolean bool = Boolean.valueOf(boolString);
          System.out.println(bool);
          
          // Now we try byte
          try {
            byte b = Byte.parseByte(testString);
            System.out.println("byte is " + b);
          } catch(NumberFormatException e) {
            System.err.println("Got a NFE in Byte.parseByte()");
          }

          // Now we try short
          try {
            short s = Short.parseShort(testString);
            System.out.println("short is " + s);
          } catch(NumberFormatException e) {
            System.err.println("Got a NFE in Short.parseShort()");
          }
        
          // Now we try int
          try {
            int i = Integer.parseInt(testString);
            System.out.println("int is " + i);
          } catch(NumberFormatException e) {
            System.err.println("Got a NFE in Integer.parseInt()");
          }
          
          // Now we try long
          try {
            long l = Long.parseLong(testString);
            System.out.println("long is " + l);
          } catch(NumberFormatException e) {
            System.err.println("Got a NFE in Long.parseLong()");
          }
              
          // Now we try float
          try {
            float f = Float.parseFloat(testString);
            System.out.println("float is " + f);
          } catch(NumberFormatException e) {
            System.err.println("Got a NFE in Float.parseFloat()");
          }

          // Now we try double
          try {
            double d = Double.parseDouble(testString);
            System.out.println("double is " + d);
          } catch(NumberFormatException e) {
            System.err.println("Got a NFE in Double.parseDouble()");
          }
        } // main()
      } // Tester
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Always trimg the string parameters.
      (Incident Review ID: 234786)
      ======================================================================

            darcy Joe Darcy
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: