0

Help ending array, I keep getting Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13. I try to end searching with && but it goes back to searching root. I need some ideas or any help on fixing this would be good!

import java.io.*;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;

public class Root_Words {    
    public static void main(String[] args) {
        String Word;

        List<String> xList = new ArrayList<String>();
        try{
            BufferedReader reader = new BufferedReader(new FileReader("Words.txt"));
            while ((Word = reader.readLine()) != null) {
                xList.add(Word);
            }
        }
        catch (IOException ioe){
            System.out.println("Problem opening input file");
        }

        String[] Words = new String[ xList.size() ];
        Collections.sort(xList);
        xList.toArray( Words );

        int c;
        String roots = "";

        for( int i = 0; i < Words.length; i++ ){
            c = root(Words[i],Words[i+1]);

            if(c >= 3 || c <=5 ){
                roots = Words[i];
                System.out.printf("%s\n", roots);

                while(Words[i].startsWith(roots) && i < Words.length){
                    System.out.printf("%s\n", Words[i]);
                    i++;
                }
                i--;
            }
        }
    }
    public static int root(String a, String b){
        int min;

        if(a.length() < b.length() ){
            min= a.length();
        }
        else{
            min = b.length();
        }
        //return min;

        int i;

        i = 0;
        while (i < min)
        {
            if (a.charAt(i) == b.charAt(i))
                i++;
            else break;

        }

        return i;


    }
}
3
  • 1
    Once i is Words.length - 1, what's i+1? And are Java arrays 0- or 1-based? You should also indicate which is line 13. And maybe indent a bit more consistently. Commented Dec 20, 2011 at 1:41
  • 1
    It would help if you'd tell us on what line the exception occurred. I count down 13 but that's the try. And post the top 5 or so items in the exception traceback. Commented Dec 20, 2011 at 1:43
  • Its not line 13, it is out of the array, its actually line 40 which is while(Words[i].startsWith(roots)){ Commented Dec 20, 2011 at 3:31

5 Answers 5

3
for( int i = 0; i < Words.length; i++ ){ 
    c = root(Words[i],Words[i+1]); 

In this snippet, when i = length - 1, then i + 1 will exceed the bounds of the array. If you are going to use i and i + 1, then the loop should likely stop at length - 1.

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

3 Comments

I am still getting it once I add that, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13 at RootWords.Root_Words.main(Root_Words.java:40) Java Result: 1
helloman, gaze down at @CharlieMartin's answer. The above certainly would have been a problem, but he accurately identifies another issue. The i++ and i-- inside your loop are most likely the culprits.
I do that so I can print the root and then the root and its children again. the output is correct how I want but not with the array like that
1

It's probably here:

for( int i = 0; i < Words.length; i++ ){
    c = root(Words[i],Words[i+1]);

When i==Words.length-1 (i.e. at the last element), [i+1] is out of range.

Comments

1

Okay, the short answer is that you've made a hash of this. I went through and normalized tthe indentation; as the others have noted, there's trouble in that while clause. But also look at this code:

    for( int i = 0; i < Words.length; i++ ){
        c = root(Words[i],Words[i+1]);

        if(c >= 3 || c <=5 ){
            roots = Words[i];
            System.out.printf("%s\n", roots);

            while(Words[i].startsWith(roots) && i < Words.length){
                System.out.printf("%s\n", Words[i]);
                i++;
            }
            i--;
        }
    }

You're using i as the index in a couple of loops, and you end up incrementing and decrementing it all over the place. This is never going to be clear, and it's almost always a sign of an error.

3 Comments

okay, so do you think i should change the variable names and then try running it? Will I have to add any more conditions
No, I think you should back up and figure out exactly what you mean to do. If you get clear about what you want to do, then you will be able to make the code work.
I guess that is where I am confused then, because I thought this was going to work fine. I am trying to print the root then children. Run, Run, Runner, Running. Any thoughts where I should look at?
0

Try reordering:

while(Words[i].startsWith(roots) && i < Words.length)

to

while(i < Words.length && Words[i].startsWith(roots))

2 Comments

What difference does that make??
&& is a shortcut operator. If the left hand side evaluates to false, it never does the right had side at all.
0

Your problem is here:

for( int i = 0; i < Words.length; i++ ){
    c = root(Words[i],Words[i+1]); // oops!

You're accessing index i+1, which is off the end of the array.

Try this:

for( int i = 0; i < Words.length - 1; i++ ){
    c = root(Words[i],Words[i+1]); // oops!

3 Comments

I'm giving everyone else with this answer a +1. Merry xmas :)
Oh dang and all I did was comment!
I tried that out but still get the same code Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13 at RootWords.Root_Words.main(Root_Words.java:40) which is pointing to error while(Words[i].startsWith(roots)){

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.