簡體   English   中英

我的代碼中StringIndexOutOfBoundsException的原因是什么

[英]What is the cause of an StringIndexOutOfBoundsException in my code

在我的學校作業中,我必須制作遞歸方法,並且只能使用.charAt,.indexOf,.substring,.toLowerCase,.concat和我在代碼中使用的其他一些方法。

這是代碼

/*
 * Lab to perform different functions on Strings
 * all methods are static
 * only two methods should be public
 * all other methods are internal (only used in the class)
 */

package stringutil;

/**
 * @author [REDACTED]
 */

public class StringUtil {

    public static boolean inOut(String input){//the argument is in main
        int len = input.length();
        boolean test;

        input = input.toLowerCase();

        //call the cleaners
        input = StringUtil.cleanse(input, len);

        //this is le final product
        String reverse = StringUtil.flip(input);
        test = input.equals(reverse);

        return test;
    }

    private static String cleanse(String raw, int count){
        if (count < 0)
            return ("");
        //this means that there was invalid punctuation
        else{
            char ch;
            ch = raw.charAt(count);

            if (ch >= 97 && ch <= 122 || ch >= 48 && ch<= 57){
                //call method again with count-1 | string is same
                return cleanse(raw, count-1);
            }
            else{ //character ain't ok yo
                if (raw.indexOf(count) == -1){
                    raw = raw.substring(raw.length()-count, count-1);
                }
                else
                    raw = raw.substring(0,count-1).concat(raw.substring(count+1));
                return cleanse(raw, count);
            }
        }
    }

    public static String flip(String input){
        String newer;
        // base case
        if (input.length() == 1){
            return input;
        }
        else{
        //take the last letter and make it the new start
            newer = input.substring(input.length()-1);
            input = input.substring(0, input.length()-1);
            return newer + flip(input);
        }
        //input = newer +
        // flip(input.substring(0, input.length()-1));
    }

/**

* @param args the command line arguments

*/

    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println(StringUtil.flip("aashf"));
        System.out.println(StringUtil.inOut("what, t;haw"));
    }
}

所以我在跑步后得到這個

fhsaa
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 11
    at java.lang.String.charAt(String.java:646)
    at stringutil.StringUtil.cleanse(StringUtil.java:36)
    at stringutil.StringUtil.inOut(StringUtil.java:21)
    at stringutil.StringUtil.main(StringUtil.java:78)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

我已經擺弄了清理不是字母或數字的字符的方法,但系統似乎喜歡從字符串到字符的轉移。

我的翻轉方法有效,但我的清理總是遇到一個超出范圍的錯誤。 我嘗試添加許多東西以確保它在范圍內,但這只會增加問題。

是的。字符串的.length()返回字符串中的一個或多個字符,因此你總是檢查越界,因為你需要在得到長度之后做減去一個,以便不超出范圍因為.charAt()返回特定地點的信

有幾個邏輯錯誤; 我想這就是你想要做的:

/*
 * Lab to perform different functions on Strings
 * all methods are static
 * only two methods should be public
 * all other methods are internal (only used in the class)
 */

package stringutil;

/**
 * @author [REDACTED]
 */

public class StringUtil {

    public static boolean inOut(String input) {// the argument is in main
        int len = input.length();
        boolean test;

        input = input.toLowerCase();

        // call the cleaners
        input = StringUtil.cleanse(input, len - 1);

        // this is le final product
        String reverse = StringUtil.flip(input);
        test = input.equals(reverse);

        return test;
    }

    private static String cleanse(String raw, int count) {
        if (count < 0)
            return raw;
        // this means that there was invalid punctuation
        else {
            char ch;
            ch = raw.charAt(count);

            if (ch >= 97 && ch <= 122 || ch >= 48 && ch <= 57) {
                // call method again with count-1 | string is same
                return cleanse(raw, count - 1);
            } else { // character ain't ok yo
                raw = raw.substring(0, count).concat(raw.substring(count + 1));
                return cleanse(raw, count - 2);
            }
        }
    }

    public static String flip(String input) {
        String newer;
        // base case
        if (input.length() == 1) {
            return input;
        } else {
            // take the last letter and make it the new start
            newer = input.substring(input.length() - 1);
            input = input.substring(0, input.length() - 1);
            return newer + flip(input);
        }
        // input = newer +
        // flip(input.substring(0, input.length()-1));
    }

    /**
     * 
     * @param args
     *            the command line arguments
     * 
     */

    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println(StringUtil.flip("aashf"));
        System.out.println(StringUtil.inOut("what, t;ahw"));
    }
}

基本上,你的// character ain't ok yo代碼段是有缺陷的,你的cleanse呼叫也是如此。 我還沒有檢查出你的其余代碼,但是main調用的代碼部分似乎現在正在運行。 此外,作為前TA,請考慮在您的代碼中添加注釋。

PS:我還將輸入字符串更改為輸出true以調用inOut

如果檢查,問題是使用此else代碼塊

raw = raw.substring(0,count-1).concat(raw.substring(count+1));

您正在嘗試使用子字符串連接一個位置,該位置超出范圍。 count是String的長度,並且您嘗試使用count + 1位置創建子字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM