2

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.

0

4 Answers 4

2

Parse it as a Long and if it works parse it as an Integer, if it throws and Exception at that point, it overflows.

OR

Parse it as a Long and check if it is bigger than Integer.MAX_VALUE

Sign up to request clarification or add additional context in comments.

Comments

2

You could parse it as a BigInteger and check if it's larger than Integer.MAX_VALUE:

BigInteger maxIntBigInteger = BigInteger.valueOf(Integer.MAX_VALUE);
String str = "214748364781";
BigInteger bi = new BigInteger(str);
if (bi.compareTo(maxIntBigInteger) > 0) {
    System.out.println("Number is too big");
} else {
    int res = bi.intValue();
    System.out.println(res);
}

Comments

1

Just to give you a different option, if all you want to do is check if your string will not overflow you could compare lexicographically. String comparison is generally much faster than parsing to a number.

Assuming only positive integers.

    String number = "2147483649";
    String int_max = "2147483647";

    if (number.length() > int_max.length())
    {
        System.out.println("OVERFLOW");
    }
    else if (number.length() == int_max.length()){
        int comparison = number.compareTo(int_max);
        System.out.println(comparison > 0 ? "OVERFLOW" : "");
    }

Now, in a real life scenario, if you can potentially have big integers you should just use Long or BigInteger as suggested in other answers.

Hope this helps.

Comments

1

If you're using Java 8, you can parse it as type Long and then use Math.toIntExact(long value) to get the int value. This throws an unchecked ArithmeticException if there is an overflow.

Here is an example with your code:

String str = "214748364781";
int res = Math.toIntExact(Long.parseLong(str));

Or to simplify further:

int res = Math.toIntExact(Long.parseLong("214748364781"));

And as expected we get the integer overflow exception as follows:

Exception in thread "main" java.lang.ArithmeticException: integer overflow
    at java.lang.Math.toIntExact(Math.java:1011)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.