我尝试拆分行中的单词并检查它们是否有句号,但出现错误:
falls.falls.falls.Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at Alpha.main(Alpha.java:10)
代码:
import java.io.*;
import java.util.*;
public class Alpha
{
public static void main(String[] args)
{
String phrase = "the moon falls. the flowers grew.";
String beta = "";
String[] array = phrase.split(" ");
for (int i = 0; i < array.length; i++)
{
if (array[i].endsWith("."))
{
array[i + 1] = array[i + 1].substring(0, 1).toUpperCase() + array[i + 1].substring(1);
beta = beta + array[i];
}
System.out.print(beta);
}
}
}
(另外我不认为我会这样称呼数组的另一个词,关于如何解决这个问题的任何建议?)