Java Array Index Out of Bounds Exception saying that array is smaller than it actually is -


my program comparing 2 strings inside 2 loops. upon running, produces error:exception in thread "main" java.lang.arrayindexoutofboundsexception: 2, though arrays initialized enough space. line of code is:

if(keywords[a].equals(tokenizedstring[b])&&tokenizedstring[a].equals("1")) 

where keywords initialized:

string[] keywords={"0","add","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"}; 

where tokenizedstring initialized:

string[] tokenizedstring={"0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"}; 

why, when run it, saying 2 out of bounds when 2 arrays being used in statement bigger?

the relevant part of code:

import java.util.scanner; import java.util.stringtokenizer;  public class main {     public static void main(string[] args) {         final int keywords = 3;         string[] keywords =                         {"0", "add", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",                                         "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",                                         "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",                                         "0", "0", "0", "0", "0", "0", "0"};         scanner scan = new scanner(system.in);         string[] tokenizedstring =                         {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",                                         "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",                                         "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",                                         "0", "0", "0", "0", "0", "0"};         int stringcounter = 1;         string input = scan.nextline();         stringtokenizer st = new stringtokenizer(input, " ");         string answer = null;         while (st.hasmoreelements()) {             answer = (string) st.nextelement();             system.out.println(answer);             tokenizedstring[stringcounter] = answer;             stringcounter++;         }         string[] functions;         functions = new string[10];         functions = testforkeywords(tokenizedstring, keywords, stringcounter, keywords);         int[] numbers = new int[10];         string[] numberss = {"1", "2"};         numbers = testfornumbers(tokenizedstring, numberss, stringcounter, 2);     }      public static int[] testfornumbers(string[] tokenizedstring, string[] keywords, int inputs,                     int numbkeywords) {         int[] functions = new int[inputs + 1];         int funcounter = 0;         (int = 0; <= numbkeywords; a++) {             (int b = 0; b <= inputs; b++) {                 if (keywords[a].equals(tokenizedstring[b]) && tokenizedstring[a].equals("1")) {                     system.out.println("yay");                     functions[funcounter] = 1;                     funcounter++;                 }             }         }         return functions;     } } 

you confused value of keywords @ point error occurs.

your error occurs in testfornumbers() have local (method scope) variable keywords passed in argument. if call this, pass in numberss in position.

numberss has 2 members - has indexes 0 , 1 - trying access index 2 indeed throw exception @ runtime


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -