# SumBigIntegerOctal – CodeJava
Task Input data fits into BigInteger data type. Octal numbers ends with o. Class should be named SumBigIntegerOctal. Solution Let’s change our parseNumber method code from SumLongOctal modification. Here’s what it looked like static long parseNumber(StringBuilder number) { if (!number.isEmpty()) { String numberString = number.toString(); int numberStringLength = numberString.length(); if (numberString.endsWith("o") || numberString.endsWith("O")) { return new BigInteger( number.substring(0, numberStringLength - 1), 8 ).longValue(); } else { return Long.parseLong(numberString); } } else { [cut: 678 characters, first 593 kept]

`CTRL K`

# SumBigIntegerOctal

# Task

- Input data fits into `BigInteger` data type.
- Octal numbers ends with `o`.
- Class should be named `SumBigIntegerOctal`.

## Solution

Let’s change our `parseNumber` method code from `SumLongOctal` modification. Here’s what it looked like

    static long parseNumber(StringBuilder number) {
        if (!number.isEmpty()) {
            String numberString = number.toString();
            int numberStringLength = numberString.length();
            if (numberString.endsWith("o") || numberString.endsWith("O")) {
                return new BigInteger(
                    number.substring(0, numberStringLength - 1),
                    8
                ).longValue();
            } else {
                return Long.parseLong(numberString);
            }
        } else {
            return 0;
        }
    }

First of all let’s change the output type of our method to `BigInteger`.

    static BigInteger parseNumber(StringBuilder number) {...}

Also in case of an empty number we can’t just `return 0` anymore because it doesn’t have `BigInteger` data type. Instead we are going to return `BigInteger.ZERO`.

Here we can get rid of `.longValue()` because we just need straight `BigInteger` type.

    return new BigInteger(number.substring(0, numberStringLength - 1), 8); // .longValue();

Also we change this

    return Long.parseLong(numberString);

to this

    return new BigInteger(numberString);

for the same reason. So the `parseNumber` will look like this

    static BigInteger parseNumber(StringBuilder number) {
        if (!number.isEmpty()) {
            String numberString = number.toString();
            int numberStringLength = numberString.length();
            if (numberString.endsWith("o") || numberString.endsWith("O")) {
                return new BigInteger(
                    number.substring(0, numberStringLength - 1),
                    8
                );
            } else {
                return new BigInteger(numberString);
            }
        } else {
            return BigInteger.ZERO;
        }
    }

Let’s rework our `main` method as well. Here’s what it looks like now

    public static void main(String[] args) {
        int sum = 0;
        for (String argument : args) {
            StringBuilder number = new StringBuilder();
            for (char c : argument.toCharArray()) {
                if (!Character.isWhitespace(c)) {
                    number.append(c);
                } else {
                    sum += parseNumber(number);
                    number = new StringBuilder();
                }
            }

            sum += parseNumber(number);
        }

        System.out.println(sum);
    }

First of all, let’s change the data type of `sum` to `BigInteger` like this

    BigInteger sum = new BigInteger("0");

Important

Notice, we can’t just do `new BigInteger()` (without any arguments) like we did with `StringBuilder`. We need to put the initial value as an argument like `new BigInteger(String val)`.

So in this stage our code will look like this

    // SumBigIntegerOctal.java

    import java.math.BigInteger;

    public class SumBigIntegerOctal {

        public static void main(String[] args) {
            BigInteger sum = new BigInteger("0");
            for (String argument : args) {
                StringBuilder number = new StringBuilder();
                for (char c : argument.toCharArray()) {
                    if (!Character.isWhitespace(c)) {
                        number.append(c);
                    } else {
                        sum += parseNumber(number);
                        number = new StringBuilder();
                    }
                }

                sum += parseNumber(number);
            }

            System.out.println(sum);
        }

        static BigInteger parseNumber(StringBuilder number) {
            if (!number.isEmpty()) {
                String numberString = number.toString();
                int numberStringLength = numberString.length();
                if (numberString.endsWith("o") || numberString.endsWith("O")) {
                    return new BigInteger(
                        number.substring(0, numberStringLength - 1),
                        8
                    );
                } else {
                    return new BigInteger(numberString);
                }
            } else {
                return BigInteger.ZERO;
            }
        }
    }

And let’s try to compile this

    $ javac SumBigIntegerOctal.java
    SumBigIntegerOctal.java:15: error: bad operand types for binary operator '+'
                        sum += parseNumber(number);
                            ^
      first type:  BigInteger
      second type: BigInteger
    SumBigIntegerOctal.java:20: error: bad operand types for binary operator '+'
                sum += parseNumber(number);
                    ^
      first type:  BigInteger
      second type: BigInteger
    2 errors

Note

We can’t use `+` operand with `BigInteger` data type. Instead we need to use `BigInteger.add(BigInteger val)`.

So we will modify like so

    // SumBigIntegerOctal.java

    import java.math.BigInteger;

    public class SumBigIntegerOctal {

        public static void main(String[] args) {
            BigInteger sum = new BigInteger("0");
            for (String argument : args) {
                StringBuilder number = new StringBuilder();
                for (char c : argument.toCharArray()) {
                    if (!Character.isWhitespace(c)) {
                        number.append(c);
                    } else {
                        sum = sum.add(parseNumber(number));
                        number = new StringBuilder();
                    }
                }

                sum = sum.add(parseNumber(number));
            }

            System.out.println(sum);
        }

        static BigInteger parseNumber(StringBuilder number) {
            if (!number.isEmpty()) {
                String numberString = number.toString();
                int numberStringLength = numberString.length();
                if (numberString.endsWith("o") || numberString.endsWith("O")) {
                    return new BigInteger(
                        number.substring(0, numberStringLength - 1),
                        8
                    );
                } else {
                    return new BigInteger(numberString);
                }
            } else {
                return BigInteger.ZERO;
            }
        }
    }

And this code will pass all the tests!