title: Anomaly in "add" method of BigInt object
body: <p>I created a <code>BigInt</code> class that stores the value as an <code>ArrayList</code> and a boolean flag to indicate whether the <code>BigInt</code> is negative or not. I have an <code>add</code> function that returns a <code>BigInt</code> object but it always returns an object with value <code>0</code> even if I manually set the <code>ArrayList</code> of the object.</p>
  <p>Here is the source code:</p>
  <pre><code>import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.List;
  
  public class BigInt {
  ArrayList&lt;Integer&gt; values; // arrayList to hold the value of the BigInt
      boolean isNegative; // boolean to tell if the BigInt is negative or not
  
  public BigInt add(BigInt other) {
      int longest = longerLength(other);
      String temp = &quot;&quot;;
      int overflow = 0;
      for (int i = 0; i &lt; longest; i++) {
          int x = values.get(i);
          int y = other.values.get(i);
          if (x + y + overflow &lt; 10) {
              temp += (x + y + overflow);
              overflow = 0;
          } else {
              temp += ((x + y + overflow) % 10);
              overflow = 1;
          }
  
      }
      if(overflow == 1)
          temp+=1;
      temp = reverse(temp);
      char[] ar = temp.toCharArray();
      BigInt sum = new BigInt(temp);
      return sum;
  }
  
  public BigInt() {
      values = new ArrayList&lt;&gt;();
      values.add(0);
      isNegative = false;
  }
  public BigInt(String str) {
      values = new ArrayList&lt;&gt;();
      char[] cA = str.toCharArray();
      if (cA[0] == '-') {
          cA = Arrays.copyOfRange(cA, 1, cA.length);
          System.out.println(Arrays.toString(cA));
          isNegative = true;
      }
      for (int i = cA.length - 1; i &gt;= 0; i--) {
          values.add(Character.getNumericValue(cA[i]));
      }
  
  }
  public String toString() {
          if(values.get(0)==0)
              return &quot;0&quot;;
          String sum = &quot;&quot;;
          
          for (int a : values) {
              sum += a;
          }
              sum += isNegative ? &quot;-&quot; : &quot;&quot;;
              sum.trim();
              return new StringBuilder(sum).reverse().toString();
          }
      private String reverse(String str) {
              char[] ca = str.toCharArray();
              String solution = &quot;&quot;;
              for (int i = ca.length-1; i&gt;=0; i--) {
                  solution += Character.getNumericValue(ca[i]);
              }
              return solution;
          }
  private int longerLength(BigInt other) {
          return values.size() &gt; other.values.size() ? values.size() : other.values.size();
      }
  }
  </code></pre>
  <p>When I run this code below, it return a <code>BigInt</code> with value <code>0</code> instead of the expected <code>110</code>.</p>
  <pre><code>import java.util.Arrays;
  
  public class Tester {
  
  public static void main(String[] args) {
  System.out.println(&quot;-----------testing add------------&quot;);
          BigInt e2 = new BigInt(55);
          BigInt e3 = new BigInt(55);
          e2 = e2.add(e3);
          System.out.println(e2.toString());
  }
  </code></pre>