Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的代码中出现StringIndexOutOfBoundsException的原因是什么_Java_Indexoutofboundsexception - Fatal编程技术网

Java 我的代码中出现StringIndexOutOfBoundsException的原因是什么

Java 我的代码中出现StringIndexOutOfBoundsException的原因是什么,java,indexoutofboundsexception,Java,Indexoutofboundsexception,在我的学校作业中,我必须使用递归方法,并且只能使用.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

在我的学校作业中,我必须使用递归方法,并且只能使用.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"));
    }
}
我曾尝试过清理非字母或数字字符的方法,但系统似乎很享受从字符串到字符的转换


我的翻转方法有效,但我的清理总是在超出范围时出错。我尝试添加许多内容以确保它在范围内,但这只会增加问题。

是的,因此
.length()
对于字符串,将返回字符串中的数量或字符,因此您总是检查出界,因为在获得长度后需要执行减号1,以避免超出范围,因为
.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"));
    }
}
/*
*实验室在字符串上执行不同的函数
*所有方法都是静态的
*只有两种方法应该是公开的
*所有其他方法都是内部的(仅在类中使用)
*/
包stringutil;
/**
*@作者[修订]
*/
公共类StringUtil{
公共静态布尔inOut(字符串输入){//参数位于main中
int len=input.length();
布尔检验;
input=input.toLowerCase();
//叫清洁工
input=StringUtil.cleanse(输入,len-1);
//这是我们的最终产品
String reverse=StringUtil.flip(输入);
测试=输入。等于(反向);
回归试验;
}
私有静态字符串清理(字符串原始、整数计数){
如果(计数<0)
返回原材料;
//这意味着有无效的标点符号
否则{
char ch;
ch=原始字符(计数);

if(ch>=97&&ch=48&&ch在if检查后,问题在于此else代码块

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

您正在尝试使用子字符串连接某个位置,这超出了界限。count是字符串的长度,您正在尝试使用count+1位置创建子字符串。

您知道所有内容都从0开始索引,对吗?是的;我是否在某个地方出错了?您对cleanse的第一次调用传递了输入及其长度,然后尝试执行
charAt(length)
,这永远是不合法的,因为有效的索引是
0…length-1
raw.indexOf(count)
应该得到什么?我这样做了,现在它在第62行
newer=input.substring(input.length()-1);
我正在调试,请给我一些时间
raw = raw.substring(0,count-1).concat(raw.substring(count+1));