我正在做一个家庭作业:
- 在字符串中找到一个唯一的元音字母,它前面有一个辅音字母,并且这个辅音字母前面有一个元音字母。
Example: "eeaaAOEacafu" Result is: u
我已经做了什么:
主类
public class Principal {
public static void main(String[] args) {
// TODO Auto-generated method stub
Stream str = new Stream();
str.setText("eeaaAOEacafu");
System.out.println(str.returnChar(str.getVowel()));
}
流类
public class Stream {
String text;
char vowel;
public String getText() {
return texto;
}
public void setText(String text) {
this.text = text;
}
public char getVowel() {
return vowel;
}
public void setVowel(char vowel) {
this.vowel = vowel;
}
public boolean isVowel(String str) {
str = str.toLowerCase();
for(int i=0; i<str.length(); i++) {
char c = str.charAt(i);
if(c=='a' || c=='e' || c=='i' || c=='o'|| c=='u') {
return true;
} else {
return false;
}
}
return false;
}
public char returnChar(String str) {
char last;
char next;
char result = '0';
int j=1;
for(int i=0; i<str.length(); i++) {
last = str.charAt(i-1);
next = str.charAt(i+1);
j++;
if(!vogal(str.charAt(i))) {
if(vogal(last) && vogal(next)) {
result = next;
}
}
}
this.setVowel(result);
return result;
} }
This returns: String index out of range: -1
这个 j=1,是为了修复这个 -1 超出范围。它修复了,但我得到了一个新的:超出范围 11 因为下一个。
问题是:我必须使用纯 java 而不能使用 API。
你们能帮帮我吗?
最佳答案
使用正则表达式进行测试和定位字符
[aeiouAEIOU][bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]([aeiouAEIOU])
关于java - 这个元音问题有什么问题?超出索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52120598/