所以我正在尝试编写一个程序来打印一个所有元音都替换为 $ 的单词。在爪哇。
当我运行它时,我不断收到这个错误exception in thread main java.lang.StringIndexOutOfBoundsException: String index out of range: 6
,它编译得很好。这是代码。
import java.util.*;
public class SummerFour
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int cnt = 0;
String word;
String output = "";
char letter = 'x';
System.out.print("enter word to test: ");
word = keyboard.nextLine();
do {
cnt++;
letter = word.charAt(cnt - 1);
if (letter == 'a' || letter == 'i' || letter == 'e' || letter == 'u' || letter == 'o')
{
letter = '$';
}
}
while (cnt <= word.length());
System.out.println(word);
}
}