Integer.parseint in Java, exception when '+' comes first

前端 未结 6 788
生来不讨喜
生来不讨喜 2020-12-19 03:31

Integer.parseInt(\"-1000\"); returns -1000 as the output.

Integer.parseInt(\"+500\"); throws an exception.

How will I be able to re

6条回答
  •  难免孤独
    2020-12-19 04:13

    One option is to use Java 7... assuming that behaves as documented:

    Parses the string argument as a signed decimal integer. 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.

    It looks like this was a new feature introduced into Java 7 though. The JDK 6 docs only indicate that - is handled.

提交回复
热议问题