2

I'm trying to implement a add function for a class like BigInteger, where the input is a crazy long number. However, when I convert from byte array to String, nothing is printing on the screen. But my sample code that is commented out in the main function works. Does anyone know why?

Here is my output on coderpad
resut byte array: [7, 9, 1, 0]
100 + 9 =

import java.io.*;
import java.util.*;
import java.nio.charset.Charset;

class Solution {
  private static Charset charset = Charset.forName("UTF-8");

  public static class BigInteger {
    public byte[] digits;

    public BigInteger(String input) {
      digits = input.getBytes(charset);
    }
    public BigInteger(byte[] digits) {
        this.digits = digits;
//         System.out.println(Arrays.toString(digits));
    }

   public String add(BigInteger other) {
        byte[] digits1, digits2;
        if (this.digits.length >= other.digits.length) {
            digits1 = this.digits;
            digits2 = other.digits;
        } else {
            digits1 = other.digits;
            digits2 = this.digits;
        }
        int remainder = 0;
        int s1 = digits1.length - 1;
        int s2 = digits2.length - 1;
        byte[] resultDigits = new byte[s1 + 2];

        for (int i = s1; i >= 0; i--) {
            int digit1 = digits1[i];
            int digit2 = i > s2 ? 0 : digits2[i];
            int r = digit1 + digit2 + remainder;
            remainder = r > 9 ? 1 : 0;
            resultDigits[i] = (byte) (r % 10);
//             System.out.println(resultDigits[i]);
        }
        resultDigits[s1] = (byte) remainder;

//         System.out.println("resut byte array: " + Arrays.toString(resultDigits));
     String result = new String(resultDigits, charset);
      return result;
    }

  }
  public static void main(String[] args) {
    BigInteger a = new BigInteger("100");
    BigInteger b = new BigInteger("9");
    System.out.println("100 + 9 = " + a.add(b));

//     String abc="123";

//     byte[] ba = abc.getBytes();
//     System.out.println(Arrays.toString(ba));
//     System.out.println(new String(ba, Charset.forName("UTF-8")));
  }
}

--- edit -- add() v2

public String add(BigInteger other) {
        byte[] digits1, digits2;
        if (this.digits.length >= other.digits.length) {
            digits1 = this.digits;
            digits2 = other.digits;
        } else {
            digits1 = other.digits;
            digits2 = this.digits;
        }
        int remainder = 0;
        int s1 = digits1.length - 1;
        int s2 = digits2.length - 1;
        byte[] resultDigits = new byte[s1 + 2];

        for (int i = s1; i >= 0; i--) {
            int digit1 = digits1[s1--];
            int digit2 = s2 >= 0 ? digits2[s2--] : 0;
            int sum = digit1 + digit2 + remainder;
            remainder = sum / 10;
            resultDigits[i] = (byte) (sum % 10);
//             System.out.println(resultDigits[i]);
        }
        resultDigits[0] = (byte) remainder; // probably dont need this

        System.out.println("resut byte array: " + Arrays.toString(resultDigits));
     String result = new String(resultDigits, charset);
     System.out.println("result as string: " + result);
      return result;
    }
7
  • Doesn't BigInteger have an add method? You might not be properly overriding it. Commented Oct 18, 2015 at 0:39
  • I'm implementing my own; this was an interview question i had Commented Oct 18, 2015 at 0:42
  • I called isEmpty() on my string and it returned false. I checked the length of it and it was 4. So I dont know why its 4 blank spaces Commented Oct 18, 2015 at 0:48
  • Are you storing the ASCII codes for '0' through '9', or the integer values 0 through 9? Commented Oct 18, 2015 at 0:49
  • You're trying to convert the bytes [7, 9, 1, 0] to a single string. Do you want to look up what each character will be? Look here. For example "9" is the tab character. Btw seeing this remainder = r > 9 ? 1 : 0; means for me that you don't know that you're working with values between 48 and 57 (incl.) for a single number. Commented Oct 18, 2015 at 0:49

1 Answer 1

1

Here is a correction :

public String add(BigInteger other) {
            byte[] digits1, digits2;
            if (this.digits.length >= other.digits.length) {
                digits1 = this.digits;
                digits2 = other.digits; 
            } else {
                digits1 = other.digits;
                digits2 = this.digits;
            }
            int remainder = 0;
            int s1 = digits1.length - 1; 
            int s2 = digits2.length - 1; 
            byte[] resultDigits = new byte[s1 + 2];

            for (int i = s1; i >= 0; i--) {
                int diff = digits1.length - digits2.length;
                int digit1 = digits1[i] - 48;
                int digit2 = i < diff ? 0 : digits2[i - diff] - 48;
                int r = digit1 + digit2 + remainder; 
                remainder = r > 9 ? 1 : 0;
                resultDigits[i+1] = (byte)(r % 10 + 48);
            }
            resultDigits[0] += (byte) remainder + 48;
            String result = resultDigits[0] == 48 ? new String(resultDigits).substring(1) : new String(resultDigits);
            return result;
        }

Basically, the errors were :

  • You did not took 48 off of your values knowing you were getting characters input
  • You also had troubles with the position of your smaller operand.
  • Finally, the remainder you were adding at the end was added at a bad position. You should always add it to your first index.

Outputs :

0 + 45 = 45
19 + 95 = 114
38 + 20 = 58
57 + 92 = 149
76 + 67 = 143
95 + 54 = 149
114 + 90 = 204
133 + 48 = 181
152 + 6 = 158
171 + 18 = 189
190 + 22 = 212
209 + 13 = 222
228 + 32 = 260
247 + 24 = 271
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.