2

Given following Java codes:

String columnValue = "188237574385834583453453635";
columnValue =(Long.parseLong(columnValue)>Long.MAX_VALUE?"0":columnValue);

It throws java.lang.NumberFormatException since the input value is beyond Long's maximum value. However, it there an easy way to detect whether a number in a 'string' type get out of Long's maximum value with out using try catch solution?

2
  • 1
    No long value can be greater than Long.MAX_VALUE, by definition. Commented Feb 24, 2015 at 16:53
  • Extract the source code of parseLong and instead of throwing an exception, return a boolean. Though, throwing the exception here makes sense, in my opinion. Commented Feb 24, 2015 at 16:56

4 Answers 4

8

A lazy way can be to use BigInteger.

BigInteger a = new BigInteger("188237574385834583453453635");
BigInteger b = BigInteger.valueOf(Long.MAX_VALUE);

System.out.println("a > Long.MAX_VALUE is : " + (a.compareTo(b) > 0 ? "true" : "false"));

If performance is important, you will have to test more solutions.

@SotiriosDelimanolis idea is also a good one :

Extract the source code of parseLong and instead of throwing an exception, return a boolean.

Also there are many throw in the code, some are for the format, other are for the overflow, you will have to choose the right ones.

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

1 Comment

I really don't understad why this was downvoted, when it is the only good answer.
0

A really cheesy way:

columnValue.length() > ("" + Long.MAX_VALUE).length()

Of course this does NOT cover values greater than Long.MAX_VALUE but the same number of digits, only values with more digits.

1 Comment

Fortunately, 'Long.MAX_VALUE' is 9.22e18, which means that you will only have 7.7% of false positive result on correctly formed string. The good point is that you can't have false negative. So it can make a good pre-filter for parsing.
-1

Not really.

The only naive partial solution is to compare the length of the String, but that would not work always.

1 Comment

Fortunately, 'Long.MAX_VALUE' is 9.22e18, which means that you will only have 7.7% of false positive result on correctly formed string. The good point is that you can't have false negative. So it can make a good pre-filter for parsing.
-1

You can create a utility method that wraps the try/catch and returns a boolean, something like:

import java.util.*;
import java.lang.*;
import java.io.*;

class TestClass
{
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println(TestClass.isLongString("188237574385834583453453635"));
    }

    public static boolean isLongString(final String longNumber)
    {
        boolean isLong = false;
        try
        {
            Long.parseLong(longNumber);
            isLong = true;
        }
        catch(Exception e)
        {
            // do nothing - return default false
        }

        return isLong;
    }
}

7 Comments

Whoever downvoted - it would be nice if you explained why you chose to do that.
Probably because the exception is still thrown and the OP doesn't want that.
It is not thrown - it is handled by the method itself, returning a boolean.
@OferLando, The problem here is that you will not know if a false result is caused by an overflow or a malformed string.
I find it regrettable that the parseLong function did not gives more informations when it throws exceptions (I looked to JDK 8u31 sources). In C# for example you get an OverflowException or a FormatException depending on the cause of the exception.
|

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.