2017-09-15 12 views
-4

文字列を分割し、3つ以上の母音を含む文字列を返すプログラムを作成しようとしています。私は方法の終わりに配列を返す方法を知らない。配列外の例外を受け取り続ける

私のプログラムで、明らかに私が見ることができない問題はどこにありますか?

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; 
    } 
} 
+0

'tokens [i]' ....なぜあなたは 'tokens'に少なくとも" i + 1 "要素があると思いますか? – lurker

+0

'if(isVowel(tokens [i] .charAt(j)))' 'tokens [i]は実際に存在すると思いますか? –

答えて

0

なぜ文字列をトークンに分割していますか?また、String []を呼び出すと、char []またはStringのいずれかを意味すると思います。文字列の配列を作成し、文字列の配列が単一の文字列のように動作するためです。ただ、これは、あなたが文字列あなたが代わりにtokens.lengthを使用していないのはなぜ

String[] tokens = myarray[i].split(" "); 

を分割している問題

for(int j = 0; j < myarray.length; j++) { 
        if(isVowel(tokens[i].charAt(j))){ 
          count++;      
        } 
       } 

ある "文字列" ではない "String []型"

+0

私は大変ご迷惑をおかけしておりますが、初心者は初心者であり、これは私の試験で助けになります。 – JaneSus

0

を使用myarray.length、tokens [j]ではなく、私はあなたが持っている文字列の数のカウンターです。

上記の変更を統合した後、あなたのコードは、これは任意の例外を与えるものではありませんが、の最後にnullを返すされているように私はこのコードのロジックとかなり驚いています。この

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; 
     } 

のようになります。方法は関係なく。

+0

私はあなたの言ったことを了承しました。 – JaneSus

+0

あなたのコードには多くのバグがあります。問題を引き起こしていたトークン[j]は文字列であり、各文字をチェックするためにその文字列を繰り返し処理する必要があります。 –

+0

最後に分割された単語で配列を返す方法はわかりませんでした – JaneSus