Java 我不断得到一个数组超出范围的异常
我正在尝试制作一个程序来拆分字符串,并返回带有3个或更多元音的字符串,我不断得到一个数组越界异常。我不知道如何在方法末尾返回数组 在我的程序中,我明显看不到的问题在哪里Java 我不断得到一个数组超出范围的异常,java,arrays,string,split,do-while,Java,Arrays,String,Split,Do While,我正在尝试制作一个程序来拆分字符串,并返回带有3个或更多元音的字符串,我不断得到一个数组越界异常。我不知道如何在方法末尾返回数组 在我的程序中,我明显看不到的问题在哪里 public class SplitWords { public static void main(String [] args){ Scanner scan = new Scanner(System.in); final int MIN_SIZE = 4; int
public class SplitWords {
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
final int MIN_SIZE = 4;
int size = 0;
do{
System.out.println("How many words are you typing(min " + MIN_SIZE + "): ");
size = scan.nextInt();
}while(size < MIN_SIZE);
scan.nextLine();
String[] myarray = new String[size];
for(int i = 0; i < myarray.length; i++){
System.out.println("Type the sentence in the position " + i);
myarray[i] = scan.nextLine();
}
System.out.println("The words which have 3 or more vowels are: " + findVowels(myarray));
}
public static String findVowels(String[] myarray){
for(int i = 0; i < myarray.length; i++){
String[] tokens = myarray[i].split(" ");
int count = 0;
for(int j = 0; j < myarray.length; j++) {
if(isVowel(tokens[i].charAt(j))){
count++;
}
}
if(count > 3){
break;
}
}
return null;
}
public static boolean isVowel(char ch){
switch(ch){
case 'a':
case'e':
case'i':
case'o':
case'u':
case'y':
return true;
}
return false;
}
}
公共类拆分字{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
最终整数最小值=4;
int size=0;
做{
System.out.println(“您键入了多少字(min”+min_SIZE+”)”;
size=scan.nextInt();
}而(尺寸<最小尺寸);
scan.nextLine();
字符串[]myarray=新字符串[大小];
for(int i=0;i3){
打破
}
}
返回null;
}
公共静态布尔元音(char-ch){
开关(ch){
案例“a”:
案例“e”:
案例i:
案例o:
案例“u”:
案例“y”:
返回true;
}
返回false;
}
}
为什么要将字符串拆分为令牌?同样调用String[],我猜您指的是char[]或String,因为您所做的是创建一个字符串数组,并将字符串数组视为单个字符串。只需使用“String”而不是“String[]”这就是问题所在
for(int j = 0; j < myarray.length; j++) {
if(isVowel(tokens[i].charAt(j))){
count++;
}
}
为什么不使用tokens.length而不是myarray.length,并使用tokens[j]而不是i,i是字符串数量的计数器
集成上述更改后,您的代码应该如下所示
public static String findVowels(String[] myarray){
for(int i = 0; i < myarray.length; i++){
String[] tokens = myarray[i].split(" ");
int count = 0;
for(int j = 0; j < tokens.length; j++) {
String str = tokens[j];
for (int j2 = 0; j2 < str.length(); j2++) {
if(isVowel(str.charAt(j2))){
count++;
}
if(count > 3){
break;
}
}
}
}
return null;
}
publicstaticstringfindvowels(String[]myarray){
for(int i=0;i3){
打破
}
}
}
}
返回null;
}
这并没有给出任何例外,但我对这段代码的逻辑感到非常惊讶,因为无论发生什么情况,您都会在方法末尾返回null。
tokens[I]
。。。。为什么你会认为tokens
至少有i+1
元素?在if(isvotel(tokens[i].charAt(j))
中,是什么让你认为tokens[i]
确实存在?我照你说的做了,它返回null。你的代码包含很多错误,我已经修复了导致问题的方法,tokens[j]是一个字符串,你需要迭代这个字符串来检查每个字符,你在代码中做了一些错误我不知道如何用拆分的单词返回数组这就是问题我很抱歉,伙计们,我是大学一年级的初学者,这是一个帮助,它将帮助我考试
public static String findVowels(String[] myarray){
for(int i = 0; i < myarray.length; i++){
String[] tokens = myarray[i].split(" ");
int count = 0;
for(int j = 0; j < tokens.length; j++) {
String str = tokens[j];
for (int j2 = 0; j2 < str.length(); j2++) {
if(isVowel(str.charAt(j2))){
count++;
}
if(count > 3){
break;
}
}
}
}
return null;
}