下面是我正在处理的一些代码,我想我会让自己成为一个二进制计算器,让我的生活变得轻松一些。但是,当我运行它时,出现错误,告诉我有一个Java.lang.StringIndexOutofBoundsException
。我真的不知道如何解决它,因为据我所知,我所做的一切正确:转换为二进制 - 获取IndexOutOfBoundsException
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;
}
}
}
您需要执行一些基本的调试:读你的异常的堆栈跟踪,因为它告诉你到底是哪一行导致了问题。然后,在该行之前添加一些'System.out.println'语句*,以便您可以看到您的String和您尝试访问的索引。 – VGR
我在这里有点困惑。有些人说我应该增加一个数组,另外一些人说我应该减少一个数组。我该怎么做? – arsb48
完全摆脱阵列。 – erickson