2017-02-15 7 views
-2

2つの文字列を受け取り、2つの文字列を比較して、最初の文字列が2番目の文字列に含まれているかどうかを判断します(順不同)。再帰メソッド:StringIndexOutOfBoundsException

私は位置

を追跡するためにカウント整数を使用残念ながら、私はメインを実行し、最初の単語のための「エルフ」、例えば第二のための「自己」に入る際にStringIndexOutOfBoundsException: String index out of range: 0を取得しています。

public static boolean containedWordsCheck(String firstWord, String secondWord, int count) { 
//Default rule for setting to false if the size of the first word is larger than the second 
     if (firstWord.length() > secondWord.length()) 
      return false; 
     //Default rule for setting to true if both strings are empty 
     if (firstWord.isEmpty() && secondWord.isEmpty()) 
      return true; 
     if (firstWord.charAt(0) == secondWord.charAt(count)) 
        return containedWordsCheck(firstWord.substring(1, firstWord.length()), secondWord, 0); 
     else if (firstWord.charAt(0) != secondWord.charAt(count) && count + 1 < secondWord.length()) 
        return containedWordsCheck(firstWord, secondWord, count + 1); 
     else 
        return false; 

たぶん、私の目が悪いですが、私は明確にするため、メイン

範囲外つもりですどこで見ることができない。

public static void main(String[] args) { 
    String firstWord = userWord.nextLine(); 
    String secondWord = userWord.nextLine(); 
    int position = 0; 
    if (containedWordsCheck(firstWord, secondWord, position)) 
     System.out.println("They are contained!"); 
    else 
     System.out.println("They are not contained"); 
+0

例外をスローする行はありますか? – azurefrog

+0

regexを使用してパターンマッチを検索します。はるかに簡単になります – user1211

+0

完全な機能を提供し、わかりやすくするためにはどうすればいいですか – Sangharsh

答えて

0

戻りcountは長さに等しい場合secondWord

public static boolean containedWordsCheck(String firstWord, String secondWord, int count) { 
//Default rule for setting to false if the size of the first word is larger than the second 
     if (firstWord.length() > secondWord.length() || count == secondWord.length()) 
      return false; 
     //Default rule for setting to true if both strings are empty 
     if (firstWord.isEmpty() && secondWord.isEmpty()) 
      return true; 
     if (firstWord.charAt(0) == secondWord.charAt(count)) 
        return containedWordsCheck(firstWord.substring(1, firstWord.length()), secondWord, 0); 
     else if (firstWord.charAt(0) != secondWord.charAt(count) && count + 1 < secondWord.length()) 
        return containedWordsCheck(firstWord, secondWord, count + 1); 
     else 
        return false; 
0

Th eエラーは、空文字列にcharAtを実行しようとしていることを示します。空の文字列は空のため、インデックス0はありません。文字列が空の場合に停止するチェックを追加してください:

public static boolean containedWordsCheck(String firstWord, String secondWord, int count) { 
    if (firstWord.length == 0) 
     return false; 
    //Default rule for setting to false if the size of the first word is larger than the second 
    if (firstWord.length() > secondWord.length()) 
     return false; 
    //Default rule for setting to true if both strings are empty 
    if (firstWord.isEmpty() && secondWord.isEmpty()) 
     return true; 
    if (firstWord.charAt(0) == secondWord.charAt(count)) 
       return containedWordsCheck(firstWord.substring(1, firstWord.length()), secondWord, 0); 
    else if (firstWord.charAt(0) != secondWord.charAt(count) && count + 1 < secondWord.length()) 
       return containedWordsCheck(firstWord, secondWord, count + 1); 
    else 
       return false; 
} 
+0

例外修正をありがとう、残念ながら私のコードはうまくいかないようです。 –