Java程序中的ArrayIndexOutOfBoundsException错误,我不知道如何解决它

时间:2022-11-07 19:02:39

I'm creating a program that's currently matching up elements of a String array to a String of user input, and the section I'm getting stuck on is if the user misspells an available word in the array, how do I spit out the closest suggestion (which, in the below program, is if six letters from the input share the same letters as one of the array elements)? This is the segment of code where I'm erring:

我正在创建一个程序,该程序当前正在将String数组的元素与用户输入的String相匹配,而我正在坚持的部分是如果用户拼错了数组中的可用单词,我该如何吐出最近的单词建议(在下面的程序中,如果来自输入的六个字母与一个数组元素共享相同的字母)?这是我犯错的代码段:

int t = 0;
LoopB: while (t < 1) {
    for (int i = 0; i <= langs.length; i++) {
        Scanner convLangs = new Scanner(langs[i]);
        while (! search.equalsIgnoreCase(langs[i])) {
            for (int j = 0; j <= langs[i].length(); j++) {
                while (convSch.hasNext()) {
                    String cS = convSch.next();
                    if (cS.equalsIgnoreCase(convLangs.next())) {
                        c++;
                        if (c == 6) {
                            System.out.println("Did you mean \"" + langs[i] + "\"? Yes or no?");
                            String confirm = console.next();
                            if (confirm.equalsIgnoreCase("yes")) {
                                System.out.println(langs[i]);
                                break LoopB;
                            } else if(confirm.equalsIgnoreCase("no")) {
                                System.out.println("Please check the spelling of your search and try again.");
                                String redo = console.next();
                                search = redo;
                            }
                        }
                    }
                }
            }
        }
    }
}

I keep getting the same error no matter what I do, and I've tried changing "<=" to "<" in the loops when dealing with the array lengths, but that doesn't work. I'm completely new to programming, and I know there are WAY easier ways to do this, so if anyone would be able to essentially tell me what I did wrong (and how to fix it to make it work), that'd be fantastic, but also if you have any suggestions for an easier way to write this, that'd also be very much appreciated.

无论我做什么,我都会遇到同样的错误,并且在处理数组长度时我尝试在循环中将“<=”更改为“<”,但这不起作用。我对编程完全不熟悉,而且我知道有更简单的方法可以做到这一点,所以如果有人能够基本上告诉我我做错了什么(以及如何修复它以使其工作),那就是太棒了,但如果你有任何建议可以更容易地写这个,那也非常感激。

Thank you for your time.

感谢您的时间。

1 个解决方案

#1


3  

You should definitely use "<" in your loop. The length of an array equals the number of elements. Because the index of an array starts from 0, the last possible index is length - 1.

你应该在你的循环中使用“<”。数组的长度等于元素的数量。因为数组的索引从0开始,所以最后一个可能的索引是length - 1。

When iterating over an array langs, always use

迭代数组lang时,请始终使用

for(int i = 0; i < langs.length; i++) { ... }

or in case you want to iterate backwards

或者如果你想倒退

for(int i = langs.length - 1; i >= 0; i--) { ... }

The same goes for your jvariable. If you don't use it anywhere and just want to do the same thing say n times. You need to iterate from 0 to n-1 or from 1 to n.

你的jvariable也一样。如果你不在任何地方使用它,只想做同样的事情说n次。您需要从0迭代到n-1或从1迭代到n。

#1


3  

You should definitely use "<" in your loop. The length of an array equals the number of elements. Because the index of an array starts from 0, the last possible index is length - 1.

你应该在你的循环中使用“<”。数组的长度等于元素的数量。因为数组的索引从0开始,所以最后一个可能的索引是length - 1。

When iterating over an array langs, always use

迭代数组lang时,请始终使用

for(int i = 0; i < langs.length; i++) { ... }

or in case you want to iterate backwards

或者如果你想倒退

for(int i = langs.length - 1; i >= 0; i--) { ... }

The same goes for your jvariable. If you don't use it anywhere and just want to do the same thing say n times. You need to iterate from 0 to n-1 or from 1 to n.

你的jvariable也一样。如果你不在任何地方使用它,只想做同样的事情说n次。您需要从0迭代到n-1或从1迭代到n。