user2610661
user2610661

Reputation: 3

storing a string into an array causes an ArrayIndexOutOfBoundsException error

int x = 0;
String[] QEquivalent = {};

String s = sc.nextLine();
String[] question2 = s.split(" ");

for (int i = 0; i < question2.length; i++) {
    System.out.println(question2[i]);
    x++;
}                                                                   //debug
System.out.println(x);

String s2 = sc2.nextLine();
String[] Answer = s2.split(" ");

for (int c = 0; c < Answer.length; c++) {
    System.out.println(Answer[c]);
}                                                                   //debug
int y;

String u = sn.nextLine();
String[] t = u.split(" ");
for (y = 0; y < question2.length; y++) {
    for (int w = 0; w < t.length; w++) {
        if (t[w].equals(question2[y])) {
            QEquivalent[y] = "ADJ";
            System.out.println(QEquivalent[y]);
            break;
        }
    }
}

this is the line of codes that I have as of now. when a string in question2 is found in String[] t, it should store the string "ADJ" in String[] QEquivalent. I can't seem to fix the error. can someone please help me?

Upvotes: 0

Views: 116

Answers (7)

Rohit Jain
Rohit Jain

Reputation: 213193

You are creating an empty array here:

String[] QEquivalent = {};

So, any index you try to access will be out of bounds. You should creating an array using a fixed size.

Or, you can better use an ArrayList instead, which can dynamically grow in size:

List<String> qEquivalent = new ArrayList<String>();

and then add elements using:

qEquivalent.add("ADJ");

And please follow Java Naming conventions. Variable names should start with lowercase letters.

Upvotes: 3

AllTooSir
AllTooSir

Reputation: 49362

Your array QEquivalent is an empty array . It is of length 0 , hence even QEquivalent[0] will throw ArrayIndexOutOfBoundsException.

One fix I can see is assign it a length :

String[] question2 = s.split(" ");
// Just assign the dimension till which you will iterate finally
// from your code `y < question2.length` it seems it should be question2.length
// Note you are always indexing the array using the outer loop counter  y 
// So even if there are n number of nested loops , assigning the question2.length
// as dimension will work fine , unless there is something subtle you missed 
// in your code
String[] QEquivalent = new String[question2.length];

Better use any implementation of List , like an ArrayList.

List<String> qEquivalent = new ArrayList<String>();
......
if (t[w].equals(question2[y])) {
       qEquivalent.add("ADJ");
       System.out.println(qEquivalent.get(y));
       break;
}

Upvotes: 1

Bumptious Q Bangwhistle
Bumptious Q Bangwhistle

Reputation: 4759

Or move it after you split the string into question2 and use:

String[] QEquivalent = new String[question2.length];

Upvotes: 0

ihsan kocak
ihsan kocak

Reputation: 1581

Possibly QEquivalent variable makes the error.Because when you declare that variable, its length is 0.So declare the variable as with new and a size.

Upvotes: 0

Mena
Mena

Reputation: 48404

You are declaring your QEquivalent array as an empty String array.

When you access the index QEquivalent[y], that index doesn't exist, hence the ArrayIndexOutOfBoundsException.

I strongly suggest you use a List<String> instead.

Such as:

List<String> qEquivalent = new ArrayList<String>(); // replaces the array declaration and uses Java conventional naming

...

qEquivalent.add("ADJ"); // replaces the indexing of the array and adds the item

Upvotes: 0

Jack
Jack

Reputation: 133557

You create an empty array:

String[] QEquivalent = {};

and then set some elements at index y > 0:

QEquivalent[y] = "ADJ";

You can either:

  • compute the final dimension of the array and be sure to instantiate it: String[] QEquivalent = new String[SIZE];
  • use a dynamic structure like an ArrayList

eg:

ArrayList<String> QEquivalent = new ArrayList<QEquivalent>(); 
QEquivalent.add("ADJ");

Upvotes: 1

Prasad Kharkar
Prasad Kharkar

Reputation: 13556

Give some size to the array String[] QEquivalent = new String[100];

You statement String[] QEquivalent = {}; creates an array with zero size.

Upvotes: 0

Related Questions