【问题标题】:Index out of bounds issue with number systems code数字系统代码的索引越界问题
【发布时间】:2020-05-15 14:57:41
【问题描述】:

我目前遇到的问题是我的 java 索引超出范围。具体错误在我的 binaryToDecimal 方法中,是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]);

        }

    }

}

【问题讨论】:

  • 您的代码中有一个无限循环。了解调试的基础知识,这样您就可以逐步执行代码并了解出现错误的原因。
  • Java 数组是基于 0 的,所以如果你的数组大小是 32,那么只有 0-31 是有效的索引。
  • while (choice > 0) {} 循环要么永远运行要么根本不运行,因为不要在循环内修改选择。

标签: java arrays binary


【解决方案1】:

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

要查看输出,我建议将数组设为 33 而不是 32。这不是正确的修复方法,但您应该能够看到它在做什么。

如果它仍然超出范围,我会仔细查看您的 while 循环结束条件。

【讨论】:

  • while 循环建议到底是什么意思?试了33个大阵还是不行
  • 如果增加数组的大小并没有“修复”数组越界问题,那么您遇到的问题比“仅仅”一个错误更基本。尝试将逻辑分解为更小的步骤,并确定哪行代码引发了异常以及引发异常的原因。
【解决方案2】:

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

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-18
    • 2014-04-27
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 2015-06-29
    • 2016-08-26
    • 2013-01-17
    相关资源
    最近更新 更多