title: Why am I getting index out of bounds exception?
body: <p>I've made this code to convert whole base 10 numbers into binary. The code I believe is everything that it needs to be but I can't get  <code>ArrayList</code> to work.</p>
  <p>I've spent a few hours on this site and other trying countless changes to no avail.</p>
  <p>I've gotten the code to compile without errors but as soon as I run it and enter an <code>int</code> the program crashes.</p>
  <p><strong>Here is the code:</strong></p>
  <pre><code>import java.util.Scanner;
  import java.util.ArrayList;
  
  public class BinaryConverter {
     public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList&lt;Integer&gt; binary = new ArrayList&lt;Integer&gt;();
        int a = in.nextInt();
        binary = binaryConverter(a);
        for (int i = binary.size(); i &gt; -1; i = i - 1){
           System.out.print(binary.get(i));
        }
    }
    public static ArrayList&lt;Integer&gt; binaryConverter(Integer n) {
     ArrayList&lt;Integer&gt; remainder = new ArrayList&lt;Integer&gt;();
     while (n/2 != 0) {
        remainder.add(n%2);
        n = n/2;
     }
     remainder.add(n%2);
     return remainder;
    }
  }
  </code></pre>
  <p>These are the exceptions java throws at me when I input a number.</p>
  <pre><code>Jons-iMac:java jon$ java BinaryConverter
  142
  
  Exception in thread &quot;main&quot; java.lang.IndexOutOfBoundsException: Index 8 out-of-bounds for length 8
      at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
      at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
      at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
      at java.base/java.util.Objects.checkIndex(Objects.java:372)
      at java.base/java.util.ArrayList.get(ArrayList.java:440)
      at BinaryConverter.main(BinaryConverter.java:16)
  </code></pre>
  <p>I hope this is enough information.</p>