Ich versuche, ein Programm, das Strings spaltet, und gibt die Saiten mit 3 oder mehr Vokalen und ich bekomme immer eine Array außerhalb der Grenze Ausnahme. Ich weiß nicht, wie man ein Array am Ende der Methode zurückgibt.Ich bekomme immer ein Array außerhalb der Grenze Ausnahme
Wo ist das Problem in meinem Programm, das ich anscheinend nicht sehen kann?
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;
}
}
'tokens [i]' .... warum würden Sie annehmen, dass 'token' mindestens' i + 1' Elemente haben? – lurker
In 'if (isVowel (tokens [i] .charAt (j)))' was lässt dich denken, dass 'tokens [i]' tatsächlich existiert? –