1

Hy,

I have the following code:

 package regexsimple5;

    import java.util.Scanner;
    import java.util.ArrayList;
    import java.util.regex.*;
    import java.io.*;
    import java.util.regex.Pattern;

    public class RegexSimple5 {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {

            ArrayList <String> foundName = new ArrayList<String>();
            ArrayList <String> noDuplicatesName = new ArrayList<String>();

            try{
                Scanner myfis = new Scanner(new File("D:\\myfis5.txt"));

                while(myfis.hasNext())
                {
                    String delim = " ";
                    String line = myfis.nextLine();     
                    String [] words = line.split(delim);

                    for( String s: words)
                    {
                        if(!s.isEmpty()&&s!=null)
                        {
                                Pattern search = Pattern.compile("[A-Z][a-z]*");
                                Matcher match = search.matcher(s);
                                if(match.find())
                                {
                                    foundName.add(s);
                                }
                        }
                    }






                }

                if(!foundName.isEmpty())
                {
                    for(String s: foundName)
                    {
                        System.out.println(s);
                        int n = foundName.size();
                        System.out.println(foundName.get(0));
                    }
                   int n = foundName.size();
                    for(int i=0; i<n; i++)
                    {
                        if(foundName.get(i).equals(foundName.get(i+1)))
                        {
                            noDuplicatesName.add(foundName.get(i));
                        }
                    }
                    System.out.println(n);
                }

                if(!noDuplicatesName.isEmpty())
                {
                    for(String s: noDuplicatesName)
                    {
                        System.out.print("***********");
                        System.out.print(s);
                    }
                }
            }
            catch (Exception ex)
            {
                System.out.println(ex);
            }

        }

    }

with which I am tring to display persons who have the same first and last name.

But I get the error:

java.lang.IndexOutOfBoundsException:

without displaying my arraylist with duplicates name and surname.

Sincerly,

SocketM
  • 564
  • 1
  • 19
  • 34

1 Answers1

1

Problem line is most likely this:

if(foundName.get(i).equals(foundName.get(i+1)))

When at the end of the list it will cause OutOfBoundsException while accessing (i+1)th element.

Difficult to understand whole code but you can probably fix it by running your loop till n-1, i.e.:

for(int i=0; i<n-1; i++)
anubhava
  • 761,203
  • 64
  • 569
  • 643