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;
}
BigIntegerhave anaddmethod? You might not be properly overriding it.[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 thisremainder = 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.