1

I'm trying to read a very large input as String and then converting it into long as per follows:[The program works for short input]

The input is two int separated by a space, example: "1248614876148768372675689568324619856329856295619253291561358926935829358293587932857923857934572895729511 413241"

My Code:

import java.io.*;
import java.math.*;
import java.util.*;
public class Solution {
    public static void main(String args[] ) throws Exception {
        Solution obj = new Solution();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        String input[]=new String[T];
        for (int i=0;i<T;i++) {
            input[i] = br.readLine();
            }
        for (int i=0;i<T;i++){
            StringTokenizer st = new StringTokenizer(input[i]," ");
                BigInteger N = new BigInteger(st.nextToken());
                BigInteger P = new BigInteger(st.nextToken());
                System.out.println(obj.result(N,P));
            }
        }
    }

    public BigInteger result(BigInteger N, BigInteger P){
        BigInteger temp=1;
        BigInteger c=0;
        for (BigInteger i=0;i<=N;i++){
            //System.out.println(nck(N,i));
            if ((nck(N,i)%P) ==0)
                c++;
        }
    return c;
    }

    public BigInteger nck(BigInteger N, BigInteger k){
        if (k==0)
            return 1;
        else {
            BigInteger temp=1;
            BigInteger y=1;
            BigInteger z=N;
                while(k>=1){
                temp=temp*z/y;
                y++;
                z--;
                k--;
            }
        return temp;
        }
    }
}

I'm getting a java.lang.NumberFormatException

1
  • 2
    Don't significantly change the content of the question when you get up voted answers. Sep 28, 2012 at 16:29

5 Answers 5

6

You can't parse this string into a long, it's too great (greater than Long.MAX_VALUE), you need BigInteger :

BigInteger bi = new BigInteger(st.nextToken());

Following your edit :

Don't try to iterate over a bigInteger : if it's too great to fit in a long, the loop will be too long for your time. Compare it with a reasonable limit and if it's smaller, then get it as int and enter your loop:

    BigInteger MAX = new BigInteger("1000000");
    if (bi.compareTo(MAX)<0) {
        int N = bi.intValue();
        for (int i=0; i<N; i++) {
            Test...
        }
    }
3
  • Okay. But still the compiler is showing errors. I've included the math.io package. I've updated the question with my entire code.
    – user188995
    Sep 28, 2012 at 16:25
  • You clearly mean Long.MAX_VALUE, not Integer.MAX_VALUE. The max value of an integer is way smaller and the person tried to parse it to a long, not to an int ;) Feb 7, 2013 at 0:32
  • @MartinBories Yes, I wrote "into a long" and then... well... I don't know why I wrote "Integer.MAX_VALUE"... Fixed, thanks. Feb 7, 2013 at 7:18
3

If throws NumberFormatException because 248614876148768372675689568324619856329856295619253291561358926935829358293587932857923857934572895729511 is greater than Long.MAX_VALUE (which is 9223372036854775807).

0

As pointed out by others the valuse is too big for long However another problem is that there can be no spaces in the string which is being converted. There is a handy post on removeing white space from a string here

"The sequence of characters following an (optional) negative sign and/or radix specifier ("0x", "0X", "#", or leading zero) is parsed as by the Long.parseLong method with the indicated radix (10, 16, or 8). This sequence of characters must represent a positive value or a NumberFormatException will be thrown. The result is negated if first character of the specified String is the minus sign. No whitespace characters are permitted in the String." From the Docs

1
  • My bad, i didn't even look at that part. Ill leave the answer there though for others who might find it.... and subsequently read this ;)
    – Deepend
    Sep 28, 2012 at 16:25
0

The value is too big for a long to hold. Primitive Data Types

I wonder what you are trying to do with so big integer but what you might be looking for is a BigInteger.

Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like Java's primitive integer types). BigInteger provides analogues to all of Java's primitive integer operators, and all relevant methods from java.lang.Math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

It provides methods for mathematical operations. Should also note that since it's immutable, any operation you perform on a BigInteger returns a new BigInteger.

0

Remember that when changing from type int to BigInteger you are then dealing with an object rather than a primitive. The BigInteger (String val) is probably the most useful constructor.

(i.e.) BigInteger temp = new BigInteger("1");

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.