java - storing a string into an array causes an ArrayIndexOutOfBoundsException error -
int x = 0; string[] qequivalent = {}; string s = sc.nextline(); string[] question2 = s.split(" "); (int = 0; < question2.length; i++) { system.out.println(question2[i]); x++; } //debug system.out.println(x); string s2 = sc2.nextline(); string[] answer = s2.split(" "); (int c = 0; c < answer.length; c++) { system.out.println(answer[c]); } //debug int y; string u = sn.nextline(); string[] t = u.split(" "); (y = 0; y < question2.length; y++) { (int w = 0; w < t.length; w++) { if (t[w].equals(question2[y])) { qequivalent[y] = "adj"; system.out.println(qequivalent[y]); break; } } }
this line of codes have of now. when string in question2 found in string[] t, should store string "adj" in string[] qequivalent. can't seem fix error. can please me?
you creating empty array here:
string[] qequivalent = {};
so, index try access out of bounds. should creating array using fixed size.
or, can better use arraylist
instead, can dynamically grow in size:
list<string> qequivalent = new arraylist<string>();
and add elements using:
qequivalent.add("adj");
and please follow java naming conventions. variable names should start lowercase letters.
Comments
Post a Comment