java:字符串索引超出范围:6。

时间:2022-11-05 16:42:12

I get the following error in my java code:

我在java代码中有以下错误:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6

线程“main”java.lang中的异常。StringIndexOutOfBoundsException:字符串索引超出范围:6。

...which occurs at the following line:

…这发生在下面一行:

if (strFactor.charAt(j) == strProduct.charAt(k)) {

Here is the code:

这是代码:

       for (int j=0; 0<=5; j++) {
            same = false;
            for (int k=0; k<=5; k++) {
                if (strFactor.length() == 6 && strProduct.length() == 6) {
                    if (strFactor.charAt(j) == strProduct.charAt(k)) {
                        same = true;
                    }
                }
            }
            if (!same) {
                return false;
            }
        }

Anyone have any idea why the index would ever be out of range?

有人知道为什么这个指数会超出范围吗?

4 个解决方案

#1


1  

I'm no Java programmer, but 0<=5 will always evaluate to true, so j reaches 6 (and would keep going were it not for that exception).

我不是Java程序员,但是0<=5将始终是正确的,因此j达到了6(并将继续保持它不属于那个例外)。

#2


1  

for (int j=0; 0<=5; j++) { should be for (int j=0; j<=5; j++) {

for(int j = 0;0 < = 5;j++){应该是for (int j=0;j < = 5;j + +){

#3


1  

Look at the first line of your forloop. The string is going out of bounds

看看你的forloop的第一行。弦要出界了。

for (int j=0; 0<=5; j++) {

Correct the condition. Always be careful when you code !! Happy Coding

正确的条件。当你编码的时候一定要小心!!快乐的编码

#4


0  

Hey man: Your j_for is wrong...It should be for(int j=0; j<=5; j++),but you write 0<=5

你的j_for是错的…应该是(int j=0;j < = 5;j + +),但是你写0 < = 5

#1


1  

I'm no Java programmer, but 0<=5 will always evaluate to true, so j reaches 6 (and would keep going were it not for that exception).

我不是Java程序员,但是0<=5将始终是正确的,因此j达到了6(并将继续保持它不属于那个例外)。

#2


1  

for (int j=0; 0<=5; j++) { should be for (int j=0; j<=5; j++) {

for(int j = 0;0 < = 5;j++){应该是for (int j=0;j < = 5;j + +){

#3


1  

Look at the first line of your forloop. The string is going out of bounds

看看你的forloop的第一行。弦要出界了。

for (int j=0; 0<=5; j++) {

Correct the condition. Always be careful when you code !! Happy Coding

正确的条件。当你编码的时候一定要小心!!快乐的编码

#4


0  

Hey man: Your j_for is wrong...It should be for(int j=0; j<=5; j++),but you write 0<=5

你的j_for是错的…应该是(int j=0;j < = 5;j + +),但是你写0 < = 5