【发布时间】:2015-09-12 19:08:17
【问题描述】:
我不明白为什么这种方法不起作用。它可以编译,但会引发运行时错误。
这是代码。这是一个猪拉丁化器。它应该将短语拆分为单词,然后格式化这些单词,然后将它们全部重新组合到一个 ArrayList 中(我以后可能会更改它。)我不明白为什么它运行不正确。
import java.util.*;
public class OinkerSpine
{
public ArrayList<String> pigLatin (String phrase)
{
phrase = phrase.replaceAll(".", " .");
phrase = phrase.replaceAll(",", " ,");
phrase = phrase.replaceAll("!", " !");
//phrase = phrase.replaceAll("?", " ?");
phrase = phrase.replaceAll("'", " '");
String []words = phrase.split(" ");
ArrayList <String> Endphrase = new ArrayList <String> ();
final String AY = "ay";
final String YAY = "-yay";
String endword = "";
for(int i=0; i < words.length; i++)
{
String firstletter;
String restofword;
String secondletter;
if (words[i].length() == 1)
{
firstletter = words[i];
restofword = "";
}
else
{
firstletter = words[i].substring(0, 1);
restofword = words[i].substring(1);
}
boolean firstIsUpper = (firstletter.equals(firstletter.toUpperCase()));
if (firstIsUpper)
{
firstletter = firstletter.toLowerCase();
secondletter = restofword.substring(0, 1);
restofword = restofword.substring(1);
secondletter = secondletter.toUpperCase();
restofword = secondletter + restofword;
}
if (firstletter.equals("a") || firstletter.equals("e") ||
firstletter.equals("i") || firstletter.equals("o") ||
firstletter.equals("u"))
{
endword = firstletter + restofword + YAY;
}
else
{
endword = restofword + "-" + firstletter + AY;
}
endword = endword.replaceAll(" .", ".");
endword = endword.replaceAll(" ,", ",");
endword = endword.replaceAll(" !", "!");
//endword = endword.replaceAll(" ?", "?");
endword = endword.replaceAll(" '", "'");
Endphrase.add(endword);
}
return Endphrase;
}
}
这里有什么?
【问题讨论】:
-
请只显示代码的相关部分,至少尝试向我们提供错误...
-
这个异常是从哪里抛出的?