-2

の「indexOf」のメソッドを書いてほしいと頼まれましたString。このメソッドは、文字列と部分文字列を取得します。部分文字列が存在する場合は、そのインデックスを返します。

しかし、私はこのエラーを受け取り続けます:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 115
    at java.lang.String.charAt(String.java:658)
    at Strings.substring(Strings.java:132)
    at Strings.indexOf(Strings.java:153)
    at Strings.main(Strings.java:70)

これは私のコードです:

    //Calling Int indexOf() method:
    //getting a new string and a substring from the user
    System.out.println("Please enter a new string");
    str4 = scan.nextLine();

    System.out.println("Please enter a new substring");
    str4Sub = scan.nextLine();

    int test = indexOf(str4, str4Sub);
    if (test != -1){
            System.out.println("Your substring appeared first on place "
                    + indexOf(str4, str4Sub) + "on your string");
    }
    else{
        System.out.println("Sorry, your substring doesnt appear in "
                + "the given string");

    }


}



/*
 * Tests whether the two given string are the same.
 * Returns true if the strings are equal,false otherwise. 
 */

public static boolean isEqual(String s1, String s2){

    int str1_length = s1.length(); //saves the length of the first string
    int str2_length = s2.length();; //saves the length of the first string
    boolean equal = true;
    int i = 0;

    //checking whether the length of both strings is the same
    if (str1_length == str2_length) {
        while (i != str1_length){
            if (s1.charAt(i) == s2.charAt(i)){ //checking if the chars at 
                //places i on both strings are 
                //the same
                i++;
                equal = true;
            }
            else{
                equal = false;
                break;
            }
        }
    }
    else{
        equal = false;
    }
    return equal;
}



/*
 * Returns a new substring of string 'S',from place 'index' and in length 
 * of length' 
 * Returns String, newStr
 */

public static String substring(String s, int index, int length){
    String newStr = ""; //will save the new string returned

    //copying the new string in length of the given length  
    while (length != 0){
        newStr += s.charAt(index);
        index++;
        length--; 
    }
    return newStr;
}


/*
 *Returns the index of the first appearance within the string s
 *of the substring sub.
 *Returns Integer, i.  
 */

public static int indexOf(String s, String sub){

    int i = 0;
    boolean flag = false;
    String strTmp = "";

    while (i < s.length() && ( (s.length() - i )> sub.length() ) ){
        strTmp = substring(s, s.charAt(i), sub.length());
        System.out.println(strTmp);

        if (isEqual(strTmp, sub)){
            flag = true;
            break;

        }
        else{
            i++;    
            flag = false;
        }
    }   

    if (!flag){
        i = -1;
        return i;
    }
    else{
        return i;
    }

}

}

4

1 に答える 1

0

indexOf、あなたは書いた

 strTmp = substring(s, s.charAt(i), sub.length());

の 2 番目のパラメータsubstringは、実際にはintです。したがって、このメソッドに渡されると、位置iの文字sが に変換されます。intそれはあなたが予想していたよりも大きな数になるでしょう。キャラクターが何であるか、およびその長さに応じてStringStringIndexOutOfBoundsException.

その行を読むように変更する必要があります

 strTmp = substring(s, i, sub.length());
于 2013-11-04T18:19:49.187 に答える