索引数字系统代码的越界问题



我目前遇到了我的 java 索引超出界限的问题。具体错误在我的二进制到十进制方法中,Index 32 out of bounds for length 32.我试图重新组织我的代码,例如将int binaryVal[] = new int[32];声明为全局变量以修复错误,但它不起作用。如何解决此问题?

import java.util.Scanner;
public class NumberConverter {
public static Scanner input = new Scanner(System.in);   
static boolean success;
static String originalNum;
static int value = 0;
static int choice = 0;
static int intNum = 0;
static int remainder;
static char [] valueBinaryArray;
public static void main(String[] args) {
// TODO Auto-generated method stub
greeting();
getOriginalNumber();
getOutputType();
//checkValidInput();
if (value == 2 && choice == 3) {
decimalToHex();
}
else if (value == 3 && choice == 2) {
hexToDecimal();
}
else if (value == 2 && choice == 1) {
decimalToBinary();
}
else if (value == 1 && choice == 2) {
binaryToDecimal();
}
}
public static void greeting() {
System.out.println("Hello and welcome to the number systems converter");
System.out.println("Below you will be givin options as to what numbers you'd like to convert");
System.out.println("");
} 
public static String getOriginalNumber() { 
System.out.println("Enter a number:");
originalNum = input.next();
System.out.println("What type of number is this?");
System.out.println("");
System.out.println("1. Binary");
System.out.println("2. Decimal");
System.out.println("3. Hexadecimal");
value = input.nextInt();
return originalNum;
}
public static void getOutputType() {
System.out.println("Which number system would you like to convert to?");
System.out.println("");
System.out.println("1. Binary");
System.out.println("2. Decimal");
System.out.println("3. Hexadecimal");
choice = input.nextInt();
}
public static void checkValidInput() {


}
public static void decimalToHex() {
int intNum = Integer.valueOf(originalNum);
String str2 = "";
char hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while (choice > 0) {
remainder = intNum % 16;
str2 = hex[remainder] + str2;
intNum = intNum/16;
}
}
public static void hexToDecimal() {
int intNum = Integer.valueOf(originalNum);
int counter = 0;
String hexVal = "";
int digit;
while (choice > 0) {
digit = intNum % 16;
switch (digit) {
case 1:
hexVal+="F"; break;
case 2:
hexVal+="E"; break;
case 3:
hexVal+="D"; break;
case 4:
hexVal+="C"; break;
case 5:
hexVal+="B"; break;
case 6:
hexVal+="A"; break;
default:
hexVal+=Integer.toString(digit);          
}
intNum = intNum/16;
}
for (counter = hexVal.length()-1; counter>=0; counter--)
System.out.print(hexVal.charAt(counter));
}
public static void decimalToBinary() {
int intNum = Integer.valueOf(originalNum);
int counter = 0;
int binaryVal[] = new int[32];
while (choice > 0) {
binaryVal[counter++] = intNum % 2;
intNum = intNum/2;
}
for (int i = counter-1; i >= 0; i--) {
System.out.println(binaryVal[i]);
}
}
public static void binaryToDecimal() {
int intNum = Integer.valueOf(originalNum);
int counter = 0;
int binaryVal[] = new int[32];
while (choice > 0) {
binaryVal[counter++] = intNum % 2;
intNum = intNum/2;
}        
for (int i = counter-1; i >= 0; i--) {
System.out.print(binaryVal[i]);
}
}
}

如果你有一个包含 32 个项目的数组,第一个是索引 0,最后一个是索引 31。 尝试访问位置 32 的项目超出了数组的范围,这就是数组索引越界异常的原因。

要查看输出,我建议可以将数组放大 33 而不是 32。 这不是一个正确的修复程序,但您应该能够看到它在做什么。

如果它仍然超出界限,我会仔细看看你的 while 循环结束条件。

所以导致错误的是你的for语句中的条件。目前,您只是检查此检查的问题是否choice >= 0它允许 for 语句超出您指定的数组长度 (32(。为了修复此错误,您只需添加另一个检查以确保 for 循环不能超过您指定的数组长度。这可以通过将以下语句添加到 for 循环中来完成:counter < binaryVal.length.这应该可以防止 for 循环超出数组的长度,并且不应遇到任何错误。二进制到十进制方法的最终代码应如下所示:

public static void binaryToDecimal() {
int intNum = Integer.valueOf(originalNum);
int counter = 0;
int binaryVal[] = new int[32];
while (choice > 0 && counter < binaryVal.length) {
binaryVal[counter++] = intNum % 2;
intNum = intNum/2;
}
for (int i = counter-1; i >= 0; i--) {
System.out.print(binaryVal[i]);
}
}

最新更新