-1
public class HugeInteger {
    private final int SIZE = 40;
    private int[] bigArray = new int[SIZE];

    public void parse (String str) {
        String[] s = str.split("\\D+");
        for (int i = 0; i < s.length; i++) {
            bigArray[i] = Integer.parseInt(s[i]);
        }
    }

    public void printArray() {
        for (int i = 0; i < SIZE; i++) {
            System.out.print(bigArray[i]);
        }
    }
}

public class IntegerTest {

    public static void main(String[] args) {
        String userStr = "";
        Scanner input = new Scanner(System.in);

        System.out.print("Enter first HugeInteger: ");
        userStr = input.nextLine();

        HugeInteger big = new HugeInteger();
        big.parse(userStr);
        big.printArray();
    }
}

Basically if I enter a string longer than 10 digits it throws and exception saying:

Exception in thread "main" java.lang.NumberFormatException: For input string: "12345367899" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at HugeInteger.parse(HugeInteger.java:9) at IntegerTest.main(IntegerTest.java:13)

Not sure what this means or how to fix it. Thanks!

Komal12
  • 3,340
  • 4
  • 16
  • 25
Chase
  • 93
  • 1
  • 1
  • 7
  • 1
    Because it cannot be represented by an `int` – Thiyagu Mar 15 '18 at 04:47
  • If the 40 is the maximum number of digits your HugeInteger can have you should take one digit (a single character) at a time; you cannot call Integer.parseInt(). – laune Mar 15 '18 at 04:53
  • The reason it has created issue lies within the regex in use, which specifically picks the digits from the string with together and not separate digit like in your case all of the string 1234567899 as one entry of token. Instead 1,2,3,.... – Rizwan Mar 15 '18 at 05:30

3 Answers3

0

As you know the int data type is a 32-bit signed two's complement integer and

int range is -2^31 to 2^31 - 1 i.e. -2147483648 to 2147483647

I think you may use long as its a 64-bit signed two's complement integer.

0

I was just informed I have to use charAt() to parse the array. So something like this?

public void parse (String str) {
        for (int i = 0; i < str.length(); i++) {
            int x = str.charAt(i);  
            bigArray[i] = x;
        }
    }
Chase
  • 93
  • 1
  • 1
  • 7
  • This would help you but you need to make sure the character at particular index is Digit only and then u can go ahead. int x = Integer.parseInt(str.charAt(I)); – Rizwan Mar 15 '18 at 05:23
  • @Rizvan parseInt would work only for String and not for char. You should go for long imo. – chepaiytrath Mar 15 '18 at 05:37
  • @Chase the above approach will just return you the ASCII values of each character in the string 12345367899 and then add to the array. which is not you requirement I suppose. – chepaiytrath Mar 15 '18 at 05:42
0

You should use long in case your input allows such numbers which are beyond the range of int.

Code sample below :

class HugeInteger {
    private final int SIZE = 40;
    private long[] bigArray = new long[SIZE];

    public void parse (String str) {
        String[] s = str.split("\\D+");
        for (int i = 0; i < s.length; i++) {
            String x = s[i];
            bigArray[i] = Long.parseLong(x);
        }
    }

    public void printArray() {
        for (int i = 0; i < SIZE; i++) {
            System.out.print(bigArray[i]);
        }
    }
}

public class IntegerTest {

    public static void main(String[] args) {
        String userStr = "";
        Scanner input = new Scanner(System.in);

        System.out.print("Enter first HugeInteger: ");
        userStr = input.nextLine();

        HugeInteger big = new HugeInteger();
        big.parse(userStr);
        big.printArray();
    }
}
chepaiytrath
  • 678
  • 1
  • 9
  • 20