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;
}
}
}