It seems parseDouble can accept strings with trailing spaces, but parseInt and parseLong throw an exception.
eg. For this test case
@Test public void testNumberParsing() { try { Double.parseDouble("123.0 "); System.out.println("works"); }catch (NumberFormatException e) { System.out.println("does not work"); } try { Integer.parseInt("123 "); System.out.println("works"); }catch (NumberFormatException e) { System.out.println("does not work"); } try { Long.parseLong("123 "); System.out.println("works"); }catch (NumberFormatException e) { System.out.println("does not work"); } }
results
works does not work does not work
Why is there a difference in behavior? Is it intentional?
source share