---
title: SumBigIntegerOctal
weight: 6
---

# Task

- Input data fits into [`BigInteger`](https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html) 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 

```java
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`.

```java
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`](https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#ZERO).

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

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

Also we change this

```java
return Long.parseLong(numberString);
```

to this

```java
return new BigInteger(numberString);
```

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

```java
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

```java
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

```java
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)`](https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#BigInteger-java.lang.String-).

So in this stage our code will look like this

```java
// 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

```bash
$ 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)`](https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#add-java.math.BigInteger-).

So we will modify like so

```java
// 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!
