在我的学校作业中,我必须制作递归方法,并且只能使用.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)
我已经摆弄了清理非字母或数字的字符的方法,但系统似乎喜欢从字符串到字符的转移。
我的翻转方法有效,但我的清洁总是遇到一个超出范围的错误。我尝试添加许多东西以确保它在范围内,但这只会增加问题。
答案 0 :(得分:2)
Yup所以.length()
为一个字符串返回字符串中的一个或多个字符,所以你总是检查越界,因为你需要在得到长度之后做一个减1,以便不超出界限因为{ {1}}返回特定地点的字母
答案 1 :(得分:1)
有几个逻辑错误;我想这就是你想要做的事情:
$(window).resize(function(){
// Place your code here which sets the width and height of canvas.
});
基本上,您的/*
* 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
中调用的代码部分似乎现在正在运行。此外,作为前TA,请考虑在您的代码中添加注释。
P.S。:我还将输入字符串更改为对main
的调用输出true。
答案 2 :(得分:0)
问题是在检查
之后使用此else代码块raw = raw.substring(0,count-1).concat(raw.substring(count+1));
您正在尝试使用子字符串连接一个超出范围的位置。 count是String的长度,并且您尝试使用count + 1位置创建子字符串。