2017-09-04 31 views
1

我在与添加在我已经使用仅2个参数,加上第3(辅助方法),我的代码休息和看向工作时递归函数一个辅助方法的问题解。该程序使用扫描仪为字符串输入键盘,为字符输入另一个输入,然后输出该字母的出现次数。错误发生在第二个if语句和两个返回语句上。第二键盘输入之后,我发现了错误: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从长度?我没有与最后一个程序有关的问题,这是我使用第三个参数添加辅助方法的时候。 – 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的理解是,你'd使用它来设置要查找字符a的字符串的字符数量;这本来是更直接的检查为字符串逐渐变大,得到安宁high字符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.");