Given the String representation of an int, I want to check for overflow.
I know how to convert a String from int using java's built in functions like parseInt.
But what if the String is so large that it might not fit into an int?
String str = "214748364781";
int res = Integer.parseInt(str);
I tried to check the sign of the String representation, do parseInt and if the signs don't match then I can say that it does overflow since int wraps around but when I actually ran it it threw a NumberFormat exception.
NumberFormat is thrown for any bad formatted string so it's not a guarantee for overflow.
So I can check that the input string's format is correct (all numbers) and then do parseInt then if I get NumberFormat I can conclude that the error's due to overflow.
However, I feel like there should be a shorter way of doing this check.