2017-09-04 56 views
1

私は既に3つ(ヘルパーメソッド)を追加するときに、2つのパラメータを使用して、既に動作している再帰関数にヘルパーメソッドを追加することに問題があります。溶液。プログラムは文字列のキーボード入力用のスキャナと文字の別の入力用のスキャナを使用し、文字の出現数を出力します。エラーは、2番目のif文と両方のreturn文で発生します。第二のキーボード入力後、私はエラーを取得しています:ここで文字列を読み込むための再帰関数のJavaヘルパーメソッド

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1

import java.util.Scanner; 

public class recursiveString { 

    public static void main(String[] args) { 

     Scanner sc = new Scanner(System.in); 
     System.out.println("Enter a string: "); 
     String input = sc.nextLine(); 
     System.out.println("Enter a character to find number of occurences: "); 
     char character = sc.next().charAt(0); 
     System.out.println(character + " occurred " + count(input, character, input.length() - 1) + " times."); 

    } 

    public static int count(String str, char a, int high) { 

     if (str.length() == high) // set equal to high to stop the recursion from infinitely looping 
      return high; 
     if (str.charAt(str.length() - 1) != a) // if the character in the string is not equal to "a" subtract from count(substring) 
      return count(str.substring(0, str.length() - 1), a, high - 1); 
     else 
      return 1 + count(str.substring(0, str.length() - 1), a, high - 1); 
      // else add +1 to count for each instance of "a" in the string 

    } 

} 
+0

次を考えてください:あなたは 'str.length()を呼び出す場合は、空の文字列は何につながる - 1 '?そして再帰を止める時を考えると、明らかに空の文字列でも 'count'を呼び出します。 – AKSW

+0

文字列はスキャナから来て、入力の長さとそれからの1を取りますか?最後のプログラムで問題はありませんでした。それは、3番目のパラメータを使用してヘルパーメソッドを追加するときです。 – Devin

+2

小さな文字列 'aba'のペン+ペーパーはあなたに問題を示すはずです - 時にはこの古い学校のデバッグが十分速く動作することがあります。それ以外の場合は、IDEにデバッガがあります。プログラムの実行を許可します。 – AKSW

答えて

1

は、あなたが範囲外のインデックスを回避するのに役立つ可能性のある解決策、です:

public static int count(String str, char a, int high) { 

    if (str == null || str.length() == 0) { 
    // just to be extra safe, if we have an empty string or null 
     return 0; 

    } 
    //changed this end condition - now high describes how many steps we take before returning the answer 
    if (high == 0) // to stop the recursion from infinitely looping 
     return high; 
    if (str.charAt(str.length() - 1) != a) // if the last character in the string is not equal to "a" subtract from count(substring) 
     return count(str.substring(0, str.length() - 1), a, high - 1); 
    else 
     return 1 + count(str.substring(0, str.length() - 1), a, high - 1); 
     // else add +1 to count for each instance of "a" in the string 

} 
2

あなたが不足しています再帰的メソッドの設計:まず、問題に焦点を当ててベースケースのために定義するか、複数のケースがある場合に定義する必要があります。

この問題について私の見解は、基本ケースが空の文字列(それでもその前に、それはnullない保証)であるということですかhighが0に設定されている場合

highの私の理解では、あなたのことを ある あなたが文字の出現を確認したい文字列の文字数を設定するのにそれを使用します。a;文字列が大きくなるにつれてチェックする方がより簡単でしたが、aの文字列の検索意味はstr.substring(0,high)になりましたが、私はあなたのコードと似ています。

//we'll use high to "tell" the count method how many characters it will consider into the occurrences from the end of the given string 
public static int count(String str, char a, int high) { 
    //if the string isn't valid or high just tells it to stop, return 0 as there can be no occurrences of a in str 
    if(str == null || str.equals("") || high == 0) 
     return 0; 

    // if the last character in the string is not equal to a, let's just shrink the string 
    if (str.charAt(str.length() - 1) != a) 
     return count(str.substring(0, str.length() - 1), a, high - 1); 

    // otherwise add this 1 occurrence to the ones it will find in the rest of the string 
    else 
     return 1 + count(str.substring(0, str.length() - 1), a, high - 1); 
} 

mainでの呼び出しは次のようになります。

System.out.println(character+ " occurred " + count(input, character, input.length()) + " times."); 
関連する問題