运行时错误为ArrayIndexOutOfBoundsException:97

时间:2019-01-31 05:56:00

标签: java arrays

我出错了,有人可以帮帮我。我正在尝试在字符串中打印出现次数最高的元音。

void vowelCount() {
    int countO = 0 ,countU = 0,countI = 0 ,countA = 0 ,countE = 0  ;
    char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
    int[] count = new int[] {countA,countE,countI,countO ,countU};
    int maxCount = 0;
    char maximumChar = ' ';

    for (int i = 0; i < TEXT.length(); i++) {
        char ch = TEXT.charAt(i);
        if (ch == vowels[0]) {
            countA++;
        }
        if (ch ==  vowels[1]) {
            countE++;
        }
        if (ch ==  vowels[2]) {
            countI++;
        }
        if (ch ==  vowels[3]) {
            countO++;
        }
        if (ch ==  vowels[4]) {
            countU++;
        }


    }

    for( int i = 0; i< vowels.length ; i++) {
        if (count[vowels[i]] > maxCount) {
            maxCount = count[vowels[i]];
            maximumChar = vowels[i];
    }
    }
    System.out.println();
    System.out.println("The most used lowercase vowel is " + maximumChar + " for " + maxCount + " times.");
 }

Arrayindexoutofbound异常结果,我不太确定我的错误在哪里。尝试了这么长时间,错误仍然重复。

3 个答案:

答案 0 :(得分:1)

问题出在这里-if (count[vowels[i]] > maxCount) {

vowels[i]将为您提供一个char的元音。当用作从char数组获取的索引时,该字符将转换为其ASCII值,该值不会在0到4的范围内。

我会说,您应该尝试找出错误,而不是找到解决方案。您的以下代码无法达到您的期望。

for (int i = 0; i < TEXT.length(); i++) {
        char ch = TEXT.charAt(i);
        if (ch == vowels[0]) {
            countA++;
        }
        if (ch ==  vowels[1]) {
            countE++;
        }
        if (ch ==  vowels[2]) {
            countI++;
        }
        if (ch ==  vowels[3]) {
            countO++;
        }
        if (ch ==  vowels[4]) {
            countU++;
        }

    }

当您使用countX++更新变量时,它并没有修改存储在count[]数组中的值,因为您已经使用0 s对其进行了初始化,即countX

答案 1 :(得分:1)

我会说count[vowels[i]]是您的问题。 vowels[i]不在[0..4]范围内,因此您超出了数组的范围。您需要count[i]。您可以尝试以下简化代码

void vowelCount() {
    char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
    int[] count = new int[vowels.length];
    int maxCount = 0;
    char maximumChar = ' ';

    for (int i = 0; i < TEXT.length(); i++) {
        char ch = TEXT.charAt(i);
        for (int j=0; j<vowels.length; j++) {
            if (ch == vowels[j]) {
                count[j]++;
                break;
            }
        }
    }

    for (int i = 0; i<vowels.length; i++) {
        if (count[i] > maxCount) {
            maxCount = count[i];
            maximumChar = vowels[i];
        }
    }

    System.out.println();
    System.out.println("The most used lowercase vowel is " + maximumChar + " for " + maxCount + " times.");
}

答案 2 :(得分:0)

由于以下几行,您将获得ArrayIndexOutOfBoundsException

if (count[vowels[i]] > maxCount) {
        maxCount = count[vowels[i]];
        maximumChar = vowels[i];
}

这里vowels[i]有字符,当您将其用作count[vowels[i]]时,您将使用存储在vowels数组中的char的ascii值作为索引来访问其中的值。 count数组。 例外情况是会打印97,因为它是char'a'的ascii值。

您应该增加count数组数据而不是变量countO, countU, etc..变量。您还需要遍历count数组并从中找到最大数,并将vowel数组中的字符分配给maximumChar变量。

static String TEXT = "teeaaaiist";
static void vowelCount() {
        int countO = 0 ,countU = 0,countI = 0 ,countA = 0 ,countE = 0  ;
        char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
        int[] count = new int[] {countA,countE,countI,countO ,countU};
        int maxCount = 0;
        char maximumChar = ' ';

        for (int i = 0; i < TEXT.length(); i++) {
            char ch = TEXT.charAt(i);
            if (ch == vowels[0]) {
                count[0]++;
            }
            if (ch ==  vowels[1]) {
                count[1]++;
            }
            if (ch ==  vowels[2]) {
                count[2]++;
            }
            if (ch ==  vowels[3]) {
                count[3]++;
            }
            if (ch ==  vowels[4]) {
                count[4]++;
            }
        }

        for( int i = 0; i< count.length ; i++) {
            if (count[i] > maxCount) {
                maxCount = count[i];
                maximumChar = vowels[i];
            }
        }
        System.out.println();
        System.out.println("The most used lowercase vowel is " + maximumChar + " for " + maxCount + " times.");
    }
    public static void main(String[] args) {
        vowelCount();
    }