免责声明:这是硬件,但对我来说没有多大意义。我有2个类PigDriver和Piglatin。我必须使用String split,不能使用Arraylist。
PigDriver:
import java.util.*;
public class PigDriver{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String t = " ";
Piglatin p = new Piglatin();
while(t.length() > 0){
t = scan.nextLine();
t = t.toLowerCase();
p.pigConvert(t);
}
p.pigReport();
}
}
Piglatin:
public class Piglatin{
// declare a data structure to store lines of text
String[] latin = {" "," "," "};
String s = " ";
public Piglatin (){
// initialize the data structure you declared above to be empty
latin[0] = s;
}
public void pigConvert(String q){
// store each line
String[] line = q.split("[ ,.!?;:]");
System.out.println(q +" 1");
for(int s=0; s<line.length; s++){
System.out.println(line[s] +" Space");
}
s = q;
/*if a word begins with a vowel (a-e-i-o-u), then "ay" is added to the end of the word
(so idle -> idleay, and often -> oftenay); on the other hand, if a word begins with a
consonant, then the first letter is removed, and is placed at the end of the word,
followed by "ay" (so month -> onthmay, and castle -> astlecay).*/
char test;
String spot = "";
for(int i=0; i<line.length; i++){
spot = line[i];
test = spot.charAt(0);
if(test == 'a' || test == 'e' || test == 'i' || test == 'o' || test == 'u'){
line[i] = spot + "ay";
System.out.println(line[i] +" 3");
}
else{
line[i] = spot.substring(1) + spot.charAt(0) + "ay";
System.out.println(line[i] +" 5");
}
}
for(int y=0; y<line.length; y++){
latin[0] = line[y]+" ";
System.out.println(latin[0]+" 6");
}
}
public void pigReport(){
//just print the lines
for(int y=0; y<latin.length; y++){
//System.out.println(latin[y]);
System.out.println("banana");
}
}
}
在此输入上:
Now is the time,
for all good, and I mean very good men and women,
to visit their grandmothers!
产生了以下输出:
ownay isay hetay imetay
orfay allay oodgay anday iay eanmay eryvay oodgay enmay anday omenway
otay isitvay heirtay randmothersgay
我意识到它有多乱,但会感激任何帮助
答案 0 :(得分:0)
您的问题是您无法调试代码。我们不是来为你做这件事......
但是,这里有一些提示可以帮助您入门。
(始终)阅读异常消息:
StringIndexOutOfBoundsException String index out of range: 0
它到底在说什么?
StringIndexOutOfBoundsException
课程的javadoc有什么说法呢?
此表达式中出现异常:spot.charAt(0)
。我们可以从中得出结论(以及你没有告诉我们的堆栈跟踪......)在调用String.charAt
时抛出了异常。
该课程的javadoc说什么?
假设您已经完成了读取javadocs的工作......您会发现问题是您的代码在那时失败了,因为它试图获取包含ZERO的字符串的第一个字符字符;即一个空字符串。
这是怎么发生的?字符串来自哪里?为什么它是零长度?
再一次......阅读javadocs!
一旦你弄清楚如何/为什么会发生这种情况,请考虑一下你应该怎么做。
我故意没有包含javadocs的链接。你的老师应该告诉你如何找到,搜索和阅读它们。