【发布时间】:2018-04-30 03:06:02
【问题描述】:
这是 JAVA 中的猪拉丁语翻译器的代码,它只适用于一个单词,但从不适用于一个句子。似乎第 30 行的代码把一切都搞砸了,我不确定它是如何做到的,或者我该如何修复它。第 8 行和第 30 行出现 IndexOutOfBoundError。我不知道如何解决这个问题,求助。
public class Practice
{
public static void main(String[] args)
{
String a = "hello Stranger";
System.out.println(translate(a)); //8
}
private static String translate(String a)
{
String XD = a;
boolean repeat = true;
int first = 1;
int second = 0;
do
{
second = XD.indexOf(" ");
if (second == -1)
{
repeat = false;
XD = vowelFinder(a);
break;
}
else
{
XD = XD + vowelFinder(a.substring(first, second)); //30
}
first = second +1;
}while(repeat == true);
return XD;
}
private static boolean isVowel (char c)
{
if (c == 'a'|| c== 'e'|| c== 'i' || c == 'o' || c== 'u')
{
return true;
}
return false;
}
private static String vowelFinder(String s)
{
String nope = s;
for(int i = 0; i <= s.length(); i++)
{
if(isVowel(s.charAt(i)) == true)
{
nope = nope.substring(i) + "-"+nope.substring(0, i);`
return nope;
}
}
return nope;
}
}
【问题讨论】:
-
他把它们写成 cmets
-
i
标签: java string indexoutofboundsexception