JDK-8040731 : parseInt() throws exception if string to be parsed contains underline chars
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 7u51
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2014-04-13
  • Updated: 2014-04-17
  • Resolved: 2014-04-16
Related Reports
Duplicate :  
Description
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("_", "");


Comments
The functionality is question was intentionally left out of Project Coin in JDK 7. A request to add it is covered in JDK-6863378.
17-04-2014

http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29 "The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value." Underscore is not a decimal digit nor a minus or a plus.
16-04-2014