java - 元音检查 - 数组越界错误

标签 java runtime-error

我正在尝试编写一个接受小写单词的程序,将其转换为大写并将单词中的元音更改为下一个字母表。到目前为止,我已经这样做了:

import java.util.*;
class prg11
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a word in lowercase.");
        String word = sc.next();
        word = word.toUpperCase();
        int length = word.length();
        char ch[] = new char[length+1];
        for (int i = 0; i<=length; i++)
        {
            ch[i] = word.charAt(i);
            if("aeiou".indexOf(ch[i]) == 0)
            {
                ch[i]+=1;
            }
        }
        String str = new String(ch);
        System.out.println(str);
    }
}

代码编译正常。但是,当我运行程序并输入一个词时,说“嘿”,这个词只以大写字母打印。其中的元音(在本例中为“e”)不会更改为下一个字母表。 我该如何解决这个问题? TIA。

最佳答案

需要改三处,按照题目中的代码。

word = word.toUpperCase();
int length = word.length();

// yours: char ch[] = new char[length + 1];
// resulting array needs to be as same length as the original word
// if not, there will be array index out of bound issues
char ch[] = new char[length];

// yours: for (int i = 0; i<=length; i++)
// need to go through valid indexes of the array - 0 to length-1
for (int i = 0; i < length; i++) {
    ch[i] = word.charAt(i);
    // yours: if ("aeiou".indexOf(ch[i]) == 0) {
    // two problems when used like that
    // 1. indexOf() methods are all case-sensitive
    //    since you've uppercased your word, need to use AEIOU
    // 2. indexOf() returns the index of the given character
    //    which would be >= 0 when that character exist inside the string
    //    or -1 if it does not exist
    //    so need to see if the returned value represents any valid index, not just 0
    if ("AEIOU".indexOf(ch[i]) >= 0) {
        ch[i] += 1;
    }
}

这是一个简洁的版本。请注意我所做的更改。

String word = sc.next().toUpperCase();
char ch[] = word.toCharArray();
for (int i = 0; i < ch.length; i++) {
    if ("AEIOU".indexOf(ch[i]) >= 0) {
        ch[i] += 1;
    }
}

indexOf() 的 Java 文档.

public int indexOf(int ch)

Returns the index within this string of the first occurrence of the specified character.
If a character with value ch occurs in the character sequence represented by this String object,
then the index (in Unicode code units) of the first such occurrence is returned.
For values of ch in the range from 0 to 0xFFFF (inclusive), this is the smallest value k such that:

     this.charAt(k) == ch

is true. For other values of ch, it is the smallest value k such that:

     this.codePointAt(k) == ch

is true. In either case, if no such character occurs in this string, then -1 is returned.

Parameters:
    ch - a character (Unicode code point).
Returns:
    the index of the first occurrence of the character in the character sequence represented by this object,
    or -1 if the character does not occur.

关于java - 元音检查 - 数组越界错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26948439/

相关文章:

Java 将 BufferedImage 作为内联图像直接发送到电子邮件中

java - 尝试 getStringArray 时出现 NullPointerException

java - 使用制表符和换行符解析路径的正则表达式模式?

java - 具有 JPA 事务的 JAX-WS Web 服务

linux - 使用 Linux 时虚幻引擎 4 启动时崩溃并出现(核心转储)错误

java - HQL多对多查询

c - 由于排序功能导致的段错误

java - 升级 Spring,现在 Freemarker 抛出 NoSutchMethod 错误

Python - 我看不到我的错误是什么,因为窗口立即消失

ruby - 在 Ruby 中构建事件循环时如何避免无限递归?