Java-创建多行英语到拉丁语翻译程序
免责声明:这是HW,但对我来说没有多大意义。我有两门课:PigDriver和Piglatin。我必须使用字符串拆分,不能使用Arraylist 驱动器:Java-创建多行英语到拉丁语翻译程序,java,string,split,Java,String,Split,免责声明:这是HW,但对我来说没有多大意义。我有两门课:PigDriver和Piglatin。我必须使用字符串拆分,不能使用Arraylist 驱动器: import java.util.*; public class PigDriver{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); String t = " "; Piglatin p = new Piglatin(); w
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();
}
}
拉丁语:
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");
}
}
产生了以下产出:
ownay isay hetay imetay
orfay allay oodgay anday iay eanmay eryvay oodgay enmay anday omenway
otay isitvay heirtay randmothersgay
我知道这有多混乱,但如果能得到任何帮助,我将不胜感激。你的问题是,你一直在调试你的代码。我们不是来为你做这件事的 但是,这里有一些提示可以帮助您开始
StringIndexOutOfBoundsException String index out of range: 0
它到底在说什么
StringIndexOutOfBoundsException
类的javadoc对此有何评论spot.charAt(0)
。我们可以从中得出结论(以及您没有显示给我们的stacktrace…),异常是在调用String.charAt
时引发的
该类的javadoc说明了什么我故意不包括指向javadocs的链接。您的老师应该告诉您如何查找、搜索和阅读这些问题。寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有帮助。如果你不理解作业,你应该联系你的讲师。如果您理解这些要求,但完全不知所措,请再次联系您的讲师。如果您还没有完全迷路,那么请尽量问一个更具体、更回答的问题。我的主要错误是线程“main”java.lang.StringIndexOutOfBoundsException中出现异常:字符串索引超出范围:0当我运行它时,它突出显示test=spot.charAt(0);和p.pigReport();作为一个问题。它可以编译,但在运行时总是会产生这个错误。谢谢你的提示,他们确实提供了帮助,是的,我的老师告诉我如何找到javadocs
StringIndexOutOfBoundsException String index out of range: 0