mardi 15 janvier 2019

How to avoid Array Out of Bound Exception in Java

I am trying to write code in Java to check a sentence for certain keywords, when any keyword found in the sentence, it should check the sentence up to 2 words before the keyword and/or up to 3 words after. if it finds a match it outputs something.

I am writing a code to iterate through 3 ArrayLists of Strings as follows:

1- iterate through Arraylist1<String) and check if ArrayList1.get(i)= ArrayList2.get(j). if equals, proceed to 2 (below), otherwise i++.

2- Iterate through ArraList3. Check up to 2 words before ArraList1[i] and up to 3 words after ArrayList1[i]. If in this range it finds a match within ArrayList3, it should output something.

Now the problem lies with implementing the conditions of 2 words before and 3 words after. Because it needs to consider a lot of conditions depending on the size of the sentence (i.e. size of ArrayList1).

Example: if you are at 1st or 2nd word of the sentence,you need to check 3 words after only And 1 word before and 3 words after (also depending on the size of the sentence).

for (int i = 0; i < tokensList.size(); i++) {

  keywordsListLoop:

    for (int j = 0; j < keywordsList.size(); j++) {

      if (tokensList.get(i).startsWith(keywordsList.get(j)) == true)

        for (int e = i - 2; e < i + 4; e++) {
          boolean flag = false;
          // I think the rules need to go here
          for (int g = 0; g < posWordsList.size(); g++) {
            // or perhaps here :s
            if (tokensList.get(e).compareTo(posWordsList.get(g)) == 0) {

              word.set(keywordsList.get(j));
              context.write(word, one);
              flag = true;
              break; //breaks out of Inner loop (PosWords loop)
            }
          }

          if (flag)
            break; //breaks out of outter loop ( e loop) 
          break keywordsListLoop; //need to make sure this actually breaks out of keywords loop
        }
    }

  //now check next token

Aucun commentaire:

Enregistrer un commentaire