4

Below is some code I'm working on, I thought I'd make myself a binary calculator to make my life slightly easier. However, when I run it, I get an error telling me that there is a Java.lang.StringIndexOutofBoundsException. I don't really know how to fix it, because as far as I can tell, I've done everything correctly:

private static void ten()
{
    Scanner scan = new Scanner(System.in);

    System.out.println("What number would you like to convert to binary?");
    System.out.print("Enter the integer here:  ");
    int x = scan.nextInt();

    String bon = Integer.toString(x , 2);

    int myArrays [ ] = new int [ 7 ];

    myArrays[0] = bon.charAt(0); 
    myArrays[1] = bon.charAt(1); 
    myArrays[2] = bon.charAt(2); 
    myArrays[3] = bon.charAt(3); 
    myArrays[4] = bon.charAt(4); 
    myArrays[5] = bon.charAt(5); 
    myArrays[6] = bon.charAt(6); 
    myArrays[7] = bon.charAt(7); 

    for (int i = 0; i < myArrays.length; i++)
    {
        System.out.print(myArrays [ i ] + " ");
        int count = 0;
        count++;
        if (count == 10) {
            System.out.println();
            count = 0;
        }
    }

}
Idos
  • 15,053
  • 14
  • 60
  • 75
arsb48
  • 563
  • 4
  • 10
  • You need to perform basic debugging: Read the stack trace of your exception, as it tells you exactly which line is causing the problem. Then, add some `System.out.println` statements *before* that line, so you can see both your String and the index you're trying to access. – VGR Feb 12 '16 at 22:24
  • I'm slightly confused here. Some people are saying that I should increase my array by one, and others say I should decrease my array by one. Which one do I do? – arsb48 Feb 12 '16 at 22:27
  • Get rid of the array altogether. – erickson Feb 12 '16 at 22:31
  • and debug your own code, which should have been the first thing you should have done before coming here – user1231232141214124 Feb 12 '16 at 22:31
  • I've tried debugging my code, and I've come across a different problem entirely. My array, should all eight integers of the array be filled, prints out "49" eight times. I assumed that the array would be printed, not something else. But it makes sense now, I'll try to do it by myself. Thanks for all the help! – arsb48 Feb 12 '16 at 22:35

7 Answers7

1

Depending on the number you enter, the length of the binary string will vary. If you enter 0 or 1, you'll get "0" or "1". But your code assumes your number has 8 bits. This means that it will only work if the eighth bit is set.

It looks like you are trying to print the bits separated by spaces, followed by a new line. It would be better to use a more direct approach:

String b = Integer.toString(x, 2);
for (int idx = 0; idx < b.length(); ++idx) {
  System.out.print(b.charAt(idx));
  System.out.print(' ');
}
System.out.println();
erickson
  • 265,237
  • 58
  • 395
  • 493
0

Ok so you have an array of size 7 which only gives you 0-6 but you make a call to array[7] so increase your array size by one

  • I thought arrays started at element "0"? – arsb48 Feb 12 '16 at 22:24
  • You are correct about the array size (should be array[8]). However, that is not where he is getting an IndexOutOfBoundsException - it is at the line of code: myArrays[3] = bon.charAt(3); – pczeus Feb 12 '16 at 22:29
0

myArrays is a 7 elements len array, if you do operations from 0 to 7 you are going outside the limit..

try from 0 to 6!

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Your array size needs to be larger... int myArrays [ ] = new int [ 8 ];

Lencalot
  • 385
  • 2
  • 19
0

There are two things to note in your code:

1.

int myArrays [] = new int [7];

Should be changed to:

int myArrays [] = new int [8];

2.

You call bon.charAt() without checking that your variable bon has enough characters.

thyago stall
  • 1,654
  • 3
  • 16
  • 30
0

Java.lang.StringIndexOutofBoundsException Pretty much indicates string's length is less than the index. You should consider the length. Also there are other Out of bounds in arrays that others mentioned.

madz
  • 1,803
  • 18
  • 45
0

Try this:

Replace these 2 lines:

String bon = Integer.toString(x , 2);
int myArrays [ ] = new int [ 7 ];

With these lines (1 just to display the initial value):

String byteString = Integer.toBinaryString(x & 0xFF);
byteString = String.format("%8s", byteString).replace(' ', '0');
int[] myArrays = new int[8];
System.out.println("(" + byteString  + ")");
pczeus
  • 7,709
  • 4
  • 36
  • 51