helloman
helloman

Reputation: 41

Java Array Exception

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;


    }
}

Upvotes: 0

Views: 550

Answers (5)

Charlie Martin
Charlie Martin

Reputation: 112366

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.

Upvotes: 1

Bohemian
Bohemian

Reputation: 425003

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!

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726539

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.

Upvotes: 1

Jason S
Jason S

Reputation: 189646

Try reordering:

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

to

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

Upvotes: 0

Anthony Pegram
Anthony Pegram

Reputation: 126834

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.

Upvotes: 3

Related Questions