2017-09-04 30 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.");