0

So, in Java I have a large number in the command argument, let's say 12345678910, and I cannot use bigInteger, and I already did:

String[] g = args[1].split("");

So, my string is put in a string array. But, I cannot use:

int[] ginormintOne = new int[g.length];
   for(int n = 0; n < g.length; n++) {
      ginormintOne[n] = Integer.parseInt(g[n]);
   }

It gives me this:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Ginormint.main(Ginormint.java:67)

I think my numbers are too large for int. Any idea on how to convert it to a large number array?

4
  • Any reason not to use java.lang.Long ? it is 64-bit signed Commented Mar 14, 2013 at 19:29
  • Let's assume that somebody puts in an arbitrarily large number, taking up more than 64 bits. How would I do it then? Commented Mar 14, 2013 at 19:41
  • BigDecimal * 10^x (scientific notation) Commented Mar 14, 2013 at 19:46
  • You can't do it without BigInteger or any other class with the same functionality. Commented Mar 14, 2013 at 19:46

3 Answers 3

5

You are splitting on an empty string. For example,

"123 456".split("")

results in this array:

["" "1" "2" "3" " " "4" "5" "6"]

This is where your exception comes from.

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

Comments

0

The first element of the array from args[1].split("") is an empty string that to my opinion causes the exception java.lang.NumberFormatException since it cannot be converted to an Integer

Comments

0

Use Long.parseLong instead of Integer.parseInt and a long[] instead of Long.parseLong.

But that said, the NumberFormatException indicates the failure is because you're passing it an empty string. Are you sure you're splitting the string correctly, or that splitting is even necessary? The args array in main is already split on spaces, assuming that's where args is coming from.

Comments

Your Answer

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