简体   繁体   中英

ArrayIndexOutOfBoundsException error in Java program, and I don't know how to fix it

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:

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.

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.

When iterating over an array langs , always use

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 j variable. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM