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


i'm creating program that's matching elements of string array string of user input, , section i'm getting stuck on if user misspells available word in array, how spit out closest suggestion (which, in below program, if 6 letters input share same letters 1 of array elements)? segment of code i'm erring:

int t = 0; loopb: while (t < 1) {     (int = 0; <= langs.length; i++) {         scanner convlangs = new scanner(langs[i]);         while (! search.equalsignorecase(langs[i])) {             (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 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 spelling of search , try again.");                                 string redo = console.next();                                 search = redo;                             }                         }                     }                 }             }         }     } } 

i keep getting same error no matter do, , i've tried changing "<=" "<" in loops when dealing array lengths, doesn't work. i'm new programming, , know there way easier ways this, if able tell me did wrong (and how fix make work), that'd fantastic, if have suggestions easier way write this, that'd appreciated.

thank time.

you should use "<" in loop. length of array equals number of elements. because index of array starts 0, last possible index length - 1.

when iterating on array langs, use

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

or in case want iterate backwards

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

the same goes jvariable. if don't use anywhere , want same thing n times. need iterate 0 n-1 or 1 n.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -