views:

101

answers:

3

I have an array of ints ie. [1,2,3,4,5] . Each row corresponds to decimal value, so 5 is 1's, 4 is 10's, 3 is 100's which gives value of 12345 that I calculate and store as long. This is the function :

public long valueOf(int[]x) {

    int multiplier = 1;
    value = 0;
    for (int i=x.length-1; i >=0; i--) {
        value += x[i]*multiplier;
        multiplier *= 10;
    }
    return value;
}

Now I would like to check if value of other int[] does not exceed long before I will calculate its value with valueOf(). How to check it ? Should I use table.length or maybe convert it to String and send to

public Long(String s) ?

Or maybe just add exception to throw in the valueOf() function ?

+4  A: 

I hope you know that this is a horrible way to store large integers: just use BigInteger.

But if you really want to check for exceeding some value, just make sure the length of the array is less than or equal to 19. Then you could compare each cell individually with the value in Long.MAX_VALUE. Or you could just use BigInteger.

Travis Gockel
its a homework, that makes the instructor look even worse
medopal
A: 

Are you guaranteed that every value of x will be nonnegative?

If so, you could do this:

public long valueOf(int[]x) {

    int multiplier = 1;
    long value = 0;  // Note that you need the type here, which you did not have
    for (int i=x.length-1; i >=0; i--) {
        next_val = x[i]*multiplier;
        if (Long.MAX_LONG - next_val < value) {
          // Error-handling code here, however you 
          // want to handle this case.
        } else {
          value += next_val
        }
        multiplier *= 10;  
    }
    return value;
}

Of course, BigInteger would make this much simpler. But I don't know what your problem specs are.

danben
unfortunately I can't use either of java.Big... classes :/ And yes, I'm sure about positive signs.And long value is declared at the beginning. My fault in copy-paste :)
owca
"unfortunately I can't use either of java.Big... classes" - WHY? Is there some Java platform that doesn't support them?
Stephen C
Is your goal to re-implement the BigInteger class? You should look at how that works, because your solution is horrific if that's what you are trying to do.
Travis Gockel
+1  A: 

Short answer: All longs fit in 18 digits. So if you know that there are no leading zeros, then just check x.length<=18. If you might have leading zeros, you'll have to loop through the array to count how many and adjust accordingly.

A flaw to this is that some 19-digit numbers are valid longs, namely those less than, I believe it comes to, 9223372036854775807. So if you wanted to be truly precise, you'd have to say length>19 is bad, length<19 is good, length==19 you'd have to check digit-by-digit. Depending on what you're up to, rejecting a subset of numbers that would really work might be acceptable.

As others have implied, the bigger question is: Why are you doing this? If this is some sort of data conversion where you're getting numbers as a string of digits from some external source and need to convert this to a long, cool. If you're trying to create a class to handle numbers bigger than will fit in a long, what you're doing is both inefficient and unnecessary. Inefficient because you could pack much more than one decimal digit into an int, and doing so would give all sorts of storage and performance improvements. Unnecessary because BigInteger already does this. Why not just use BigInteger?

Of course if it's a homework problem, that's a different story.

Jay