2017-02-15 102 views
-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

使用正則表達式來查找模式匹配。它會簡單得多 – 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

謝謝你的例外修復,不幸的是,似乎我的代碼不工作,雖然。 –