2

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 ArrayList to work.

I've spent a few hours on this site and other trying countless changes to no avail.

I've gotten the code to compile without errors but as soon as I run it and enter an int the program crashes.

Here is the 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<Integer> binary = new ArrayList<Integer>();
      int a = in.nextInt();
      binary = binaryConverter(a);
      for (int i = binary.size(); i > -1; i = i - 1){
         System.out.print(binary.get(i));
      }
  }
  public static ArrayList<Integer> binaryConverter(Integer n) {
   ArrayList<Integer> remainder = new ArrayList<Integer>();
   while (n/2 != 0) {
      remainder.add(n%2);
      n = n/2;
   }
   remainder.add(n%2);
   return remainder;
  }
}

These are the exceptions java throws at me when I input a number.

Jons-iMac:java jon$ java BinaryConverter
142

Exception in thread "main" 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)

I hope this is enough information.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Jon
  • 31
  • 1
  • 1
  • 5
  • 1
    Don't start at `binary.size()`. Start at `binary.size() - 1`. – Silvio Mayolo Jan 18 '18 at 01:31
  • @Jon - as an aside, I want to point out that `ArrayList` is a choice made during implementation; you are dealing with a `List` which just-so-happens to be an ArrayList. Good practice would be to declare `List binary = new ArrayList<>();` — _I have a List which I have chosen an ArrayList to implement._ (also using the [diamond operator](https://docs.oracle.com/javase/7/docs/technotes/guides/language/type-inference-generic-instance-creation.html) to eliminate the redundant `Integer` declaration) – Stephen P Jan 18 '18 at 01:48
  • @StephenP See also [this](https://stackoverflow.com/a/41915912/573032) answer. – Roman C Jan 18 '18 at 01:52
  • When I use List instead of ArrayList I get a "cannot find symbol" error when I compile. Would that be die to me using a text editor and cmd rather than an ide? – Jon Jan 18 '18 at 01:54
  • @Jon A text editor gone to ages. Did you try [this](https://stackoverflow.com/a/12585362/573032) solution if you use IJ. – Roman C Jan 18 '18 at 02:08
  • @Jon - as Roman mentioned, an IDE will usually do this for you ... you need to `import java.util.List;` in addition to your `import java.util.ArrayList` (I use both an IDE and a plain-text editor) – Stephen P Jan 18 '18 at 18:07

2 Answers2

2
for (int i = binary.size(); i > -1; i = i - 1){

Assuming binary contains 8 elements, i first start with 8. However list/array etc in Java is 0-based, so the index should be 0-7. Of course it is going to give you error when you try to access binary.get(8)

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • This is something I should know.... That was a mega brain fart. Thanks for the help Adrian! – Jon Jan 18 '18 at 01:42
2

In your code you get the value by index which is evaluated incorrectly.

  for (int i = binary.size(); i > -1; i = i - 1){
     System.out.print(binary.get(i));
  }

Assignment of binary.size() indeed out of bounds, because indexes counted from binary.size() to zero, but it is out of range which is bound from zero to binary.size()-1.

Roman C
  • 49,761
  • 33
  • 66
  • 176