Integer.parseint in Java, exception when '+' comes first
java
Solution
Try `DecimalFormat` like with the pattern `"+#;-#"`. It will handle explicit signed parsing. Breakdown of the pattern:
- The first part (before `;`) is the positive pattern, it has to start with an `+` char
- The second part is the negative and has to start with a `-` char
Example:
DecimalFormat df = new DecimalFormat("+#;-#");
System.out.println(df.parse("+500"));
System.out.println(df.parse("-500"));
Outputs:
500
-500
Problem
`Integer.parseInt("-1000");` returns -1000 as the output. `Integer.parseInt("+500");` throws an exception. How will I be able to recognize positive numbers with the "+" symbol before them without having to trim the symbol?