我正在尝试在java中创建一种方法来判断字符串的格式是否正确。字符串中的每个字符都应等于预定义字符之一,而不应等于其他字符。 第一个字符应等于第一个数组中的值之一。 第二个字符应等于列数组中的值之一。 第三个字符应等于行数组中的值之一。 第四个字符应等于第四个数组中的值之一。 到目前为止,我有这段代码。
public static boolean formedGoodOrNot(String input) {
char[] first = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'};
int[] columns = {1, 2, 3, 4, 5, 6, 7, 8};
char[] rows = {'A', 'B', 'C', 'D'};
int[] fourth = {0, 1, 2, 3, 4, 5, 6, 7};
if(input.length()==4) {
for (int j = 0; j < first.length+1; ) {
for (int k = 0; k < columns.length+1; ) {
for (int l = 0; l < rows.length+1; ) {
for (int m = 0; m < fourth.length+1; ) {
if (input.charAt(0) == first[j]) {
if (input.charAt(1) == columns[k]) {
if (input.charAt(2) == rows[l]) {
if (input.charAt(3) == fourth[m]) {
return true;
} else{
m++;
}
} else {
l++;
}
} else{
k++;
}
} else{
j++;
}
}
}
}
}
} else{
return false;
}
return false;
}
但是,它给了我一个错误
java.lang.ArrayIndexOutOfBoundsException: 12
这是怎么回事?
谢谢
正则表达式
您应该为此目的使用正则表达式!与此模式匹配的正则表达式是:
^[a-l][1-8][A-D][0-7]$
现在你只需要把它插入一个函数:
private static final Pattern PATTERN = Pattern.compile("^[a-l][1-8][A-D][0-7]$");
public boolean formedGoodOrNot(String input) {
return PATTERN.matcher(input).matches();
}
你去吧,比你的实现更具可读性和简短性!
编辑
要了解此正则表达式的工作原理,请查看此解释它的链接: https://regex101.com/r/SDlnzi/1/
既然你的问题是这里出了什么问题?,让我们来看看:
-
在检查每个字符之前,循环是嵌套的,从而导致不必要的检查。提示:当第 1 个字符无效时,您无需输入第 2、第 3 或第 4 个字符的循环。
-
其中两个数组包含
int
s,而不是char
s,但您只需将String
中的char
s 与int
值进行比较,这是行不通的。 -
您将计数器与
length+1
而不是length
进行比较,从而越界。(这些数组的长度不是length+1
,而是length
。 -
对于任何以至少一个有效字符开头的无效
String
,您都有一个无限循环,因为您只递增else
分支中的计数器,但对于部分无效的String
没有break
/return
条件。
所有索引检查都应该是
< first.length;
不
< first.length+1;
等。。。
例如,您的第一个数组是 0..11,而您正在访问 12。
顺便说一句,欢迎来到堆栈溢出!
public static boolean formedGoodOrNot(String input) {
char[] first = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'};
int[] columns = {1, 2, 3, 4, 5, 6, 7, 8};
char[] rows = {'A', 'B', 'C', 'D'};
int[] fourth = {0, 1, 2, 3, 4, 5, 6, 7};
if(input.length()==4) {
for (int j = 0; j < first.length; ) {
for (int k = 0; k < columns.length; ) {
for (int l = 0; l < rows.length; ) {
for (int m = 0; m < fourth.length; ) {
if (input.charAt(0) == first[j]) {
if (input.charAt(1) == columns[k]) {
if (input.charAt(2) == rows[l]) {
if (input.charAt(3) == fourth[m]) {
return true;
} else{
m++;
}
} else {
l++;
}
} else{
k++;
}
} else{
j++;
}
}
}
}
}
} else{
return false;
}
return false;
}
您使用了嵌套的for
循环,这对于您的问题不是必需的。而且array.length + 1
也会导致异常引发。应该是array.length
.
char[] first = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'};
int[] columns = {1, 2, 3, 4, 5, 6, 7, 8};
char[] rows = {'A', 'B', 'C', 'D'};
int[] fourth = {0, 1, 2, 3, 4, 5, 6, 7};
boolean firstTest;
boolean columnTest;
boolean rowsTest;
boolean fourthTest;
if(input.length()==4) {
for (int j = 0; j < first.length; j++){
if (input.charAt(0) == first[j]){
firstTest = true;
break;
}
}
if(!firstTest)
return false;
for (int j = 0; j < columns.length; j++){
if (input.charAt(0) == columns[j]){
columnsTest= true;
break;
}
}
if(!columnTest)
return false;
// Do the same for other tests as well
// if all tests are passed
return true;
}
如果你想要一个更快的方法,你可以使用哈希集并节省循环的时间。
Set<Character> first= new HashSet<>(Arrays.asList('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'));
Set<Integer> columns= new HashSet<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
// create other sets
if(first.contains(input.charAt(0)) && columns.contains(input.charAt(1)) && /*other similar checks*/)
return true;
return false;