0

I know from reading other questions on the forumn that Array indexing is causing the problem, but I don't know any way around it. I commented where the throw happens. The whole throw is

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 35
at assignment4.AnagramUtil.getLargestAnagramGroup(AnagramUtil.java:82)
at assignment4.AnagramTester.main(AnagramTester.java:36)

If anyone has any ideas how I can make this work, let me know. Also, I don't think any more of my methods are relevant in solving this but I can put them here if needed.

    /*areAnagrams
     * parameters: sorted strings x & y
     * returns boolean
     * implements sort method
     */
    public static boolean areAnagrams(String x, String y)
    {
        if(sort(x).equals(sort(y)))
            return true;
        return false;
    }


    /*
     * This function takes a string array and finds the largest anagram group.
     * AnagramComparator.insertionSort() sorts the array by placing anagrams together,
     * so no sorting is needed.
     * I use ArrayList because I want to be able to freely add to the string array.
     * returns a new String[]
     */
    public static String[] getLargestAnagramGroup(String[] input)
    {
        String[] s=input;

        AnagramComparator.insertionSort(s, new AnagramComparator());

          int largestCount = 0, tempCount=1;
          ArrayList<String> largest= new ArrayList<String>();
          ArrayList<String> temp= new ArrayList<String>();

          for(int i=0; i<s.length; i++)
          {
              //since it's already sorted, we need only to compare.
              //add 
              temp.add(s[i]);
                                            //Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 35
              if (areAnagrams(s[i],s[i+1]))  //at assignment4.AnagramUtil.getLargestAnagramGroup(AnagramUtil.java:82)
              {
                  //add s[i+1] to array list
                  temp.add(s[i+1]);
                  tempCount++;
              }
              else
              {

                    //if tempcount> largestcount, empty contents of largest and make temp largest

                    if (tempCount>largestCount)
                    {
                        if(!largest.isEmpty())
                            largest.clear();
                        largest=temp;
                    largestCount=tempCount; 
                    }
                    //reset tempcount
                    tempCount=1;
              }

          }

     String[] result= new String[largest.size()];

     for (int j=0;j<largest.size();j++)
         result[j]=largest.get(j);
                return result;


    }   
5
  • What happens with this line when i = length-1; temp.add(s[i+1]); Commented Feb 14, 2015 at 3:38
  • Why do you need String[] s=input; Commented Feb 14, 2015 at 3:40
  • It still throws. It might help, I don't know. Commented Feb 14, 2015 at 3:48
  • I use String[] s= input so the insertionsort doesn't affect the input. Commented Feb 14, 2015 at 3:49
  • @JawshBivens references do not work that way. String[] s = input copies the reference, not the array - now input and s both contain references to the one array. Commented Feb 14, 2015 at 5:42

3 Answers 3

3

your problem is here:

areAnagrams(s[i],s[i+1])

this will fail when i = s.length - 1 because of i + 1 (when i = s.length it is out of bounds, as length returns the number of elements, yet array index starts at 0)

change

for(int i=0; i<s.length; i++)

to

for(int i=0; i<s.length - 1; i++)
Sign up to request clarification or add additional context in comments.

Comments

2

Try changing this line:

for(int i=0; i<s.length; i++)

to this:

 for(int i=0; i<s.length-1; i++)

Comments

0

You have a loop

for(int i=0; i<s.length; i++) {...

which means that for the maximum allowable value of i, s[i] will be the last element in s.

But within the loop you are referencing s[i + 1], which is past the end of s.

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.