0

I'm looking for the equivalent word in the database by the ContextQuery method, and when a equivalent word is null the program must try to use the next index from words and add it up to the current to make it a two word, if the two word is still null the program will make it a three word looking for the next 2 value, the equivalent is now being printed in console but i have the error IndexOutOfBoundsExpection after running

        for (int i = 0; i < words.size(); i++){
        temp = QueryWithContext.query(words.get(i));
            if((temp == null || temp.isEmpty()) && words.size() >= i+1)
            {
            QueryWithContext.query(words.get(i)+" "+words.get(i+1));
            temp = QueryWithContext.query(words.get(i)+" "+words.get(i+1));
            System.out.println("1st if");
                if((temp == null || temp.isEmpty()) && words.size() >= i+2)
                    {
                    temp = QueryWithContext.query(words.get(i)+" "+words.get(i+1)+" "+words.get(i+2));
                    }
                    else
                    {
                        temp = words.get(i);
                    }

            }



            System.out.println(temp);
3
  • 3
    Then i is too large. Step through the code in the debugger. Commented Feb 25, 2015 at 15:03
  • What about i? From where do you get it and how do you update it? Commented Feb 25, 2015 at 15:04
  • Post edited, forgot to include the for loop Commented Feb 25, 2015 at 15:05

2 Answers 2

2
if((temp == null || temp.isEmpty()) && words.size() >= i+1)

must be

if((temp == null || temp.isEmpty()) && words.size() > i+1)

otherwise

words.get(i+1)

throws the IndexOutOfBoundsExpection.

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

2 Comments

Got it thanks, and how can i move to the next for loop after it passes by the if condition
I don't understand what you want to do. Please be more specific.
2

The problem is most likely in this line: temp = QueryWithContext.query(words.get(i)+" "+words.get(i+1)+" "+words.get(i+2));. You are looping until i is less than the size of words, so i will range from 0 to n - 1.

The problem is that in your code, you keep going till i + 2 (and previously, i + 1). This is what is most likely causing your error. To fix this, see if you can do the following: for (int i = 0; i < (words.size() - 2); i++){

Alternatively, do as @Uli recommends.

Comments

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.