My program is comparing two strings inside two for loops. Upon running, it produces the error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
, even though the arrays are initialized for enough space. The line of code is:
if(keywords[a].equals(tokenizedString[b])&&tokenizedString[a].equals("1"))
Where keywords
is 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
is 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 I run it, is it saying that two is out of bounds when the only two arrays being used in that statement are obviously 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;
for (int a = 0; a <= numbKeywords; a++) {
for (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 are confused as to the value of keywords
at the point the error occurs.
Your error occurs in testForNumbers()
where you have a local (method scope) variable keywords
passed in as an argument. If you look where you call this, you pass in numberss
in this position.
numberss
has only two members - so has indexes 0
and 1
only - trying to access index 2
will indeed throw this Exception at runtime
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.