0
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?

2
  • The variables in Java should start with a lowercase. Like Answer and QEquivalent. Commented Jul 31, 2013 at 15:18
  • 1
    The String[] QEquivalent is an empty array - obviously any attempt to access its members by index will result in an java.lang.ArrayIndexOutOfBoundsException. Commented Jul 31, 2013 at 15:19

7 Answers 7

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

should I remove ={} ?
@user2610661. Either you create an array of some size, which is still unknown - String[] qEuivalent = new String[50];. Or, use an ArrayList as I suggest.
It works perfectly! Thanks alot! I'm just starting to learn java so thanks again for helping me!
@user2610661. You're welcome :) And keep consulting Java API, time to time during your learning. You might get something useful for the problem you are solving.
1

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");

Comments

1

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;
}

Comments

0

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

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

Comments

0

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

Comments

0

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.

Comments

0

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

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

4 Comments

question2.length won't be enough. There are more elements going to be added. Take a look at the loop which populates array. It is nested, with outer one itself running till question2.length.
@RohitJain But he is indexing it with j that is the outer loop counter !
@RohitJain I disagree. He uses the loop for (y = 0; y < question2.length; y++) and only assigns QEquivalent[y] = "ADJ";. So regardless of the inner loop, QEquivalent[y] will never be greater than question2.length.
@TheNewIdiot. OOPs, didn't notice that. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.