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");
例外をスローする行はありますか? – azurefrog
regexを使用してパターンマッチを検索します。はるかに簡単になります – user1211
完全な機能を提供し、わかりやすくするためにはどうすればいいですか – Sangharsh