The problem is that you are trying to construct a BigInteger
with an invalid number (03781-
). This string is created by reversing -18730
in your reverse
method.
This is what is printed without the addition:
C:\>java Digit
-18730
03781-
Exception in thread "main" java.lang.NumberFormatException: Illegal embedded sign character
at java.math.BigInteger.<init>(Unknown Source)
at java.math.BigInteger.<init>(Unknown Source)
at BigDigit.sub(Digit.java:42)
at Digit.main(Digit.java:15)
This is what's printed when I enable the addition
C:\>java Digit
59000
00095
77730
03777
Reversing a positive integer (59000
) won't leave the embedded minus sign (00095
), which is the source of the exception you are seeing.