递归方法:StringIndexOutOfBoundsException

时间:2017-02-15 19:03:50

标签: java recursion

我正在创建一个程序,它接受两个字符串然后比较两个字符串以确定第一个字符串的字符是否包含在第二个字符串中(无序)。

我使用计数整数来跟踪位置

不幸的是,我在执行main时获得StringIndexOutOfBoundsException: String index out of range: 0并在第一个单词输入“elf”,而第二个单词输入“self”。

    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");

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

该错误表示您正在尝试对空字符串执行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;
}