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

parseInt() throws exception if string to be parsed contains underline chars

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 7u51
    • core-libs
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      1.7.0_51

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP 32bit v5.1.2600
      (but reported bug is OS-independent, i guess)

      A DESCRIPTION OF THE PROBLEM :
      Integer.parseInt() method (other similar methods could also be affected) throws a NumberFormatException if a string to be parsed contains underline characters, although Java 7 specification allows underscores in numerical literals:
      http://docs.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html

      The official documentation also does NOT talk about, such prohibits underlines/underscores: http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29

      Bug originally reported, so credit goes to Mr. Ivor Horton: published in Ivor Horton's Beginning Java - Java 7 edition (in Chapter 5: Defining Classes, Page 202, grey WARNING messagebox)

      REGRESSION. Last worked in version 7u51

      ADDITIONAL REGRESSION INFORMATION:
      1.7.0_51

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the enclosed source code, but it's an obvious and understandable problem, no need for further examples.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      i:10000 , j:10000
      i:10000 , j:10000
      ACTUAL -
      i:10000 , j:10000
      Exception in thread "main" java.lang.NuberFormatException: For input string: "10_000"
      ...

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.NuberFormatException: For input string: "10_000"

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class TryInt {
      public static void main(String[] args) {
      int i = 10000;
      int j = 10_000;
      System.out.println("i:" + i + " , j:" + j); // outputs: i:10000 , j:10000

      i = Integer.parseInt("10000");
      j = Integer.parseInt("10_000");
      System.out.println("i:" + i + " , j:" + j); // Exception in thread "main" java.lang.NuberFormatException: For input string: "10_000"
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      write a method to remove underlines:
      str.replace("_", "");
      str.replaceAll("_", "");

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: