Hi, i am facing a problem with reading of duplicate element in an array. I was told to ignore the duplicate element in a text file and continue read in data from the text file at the same time store the data in an object array and diaplay it. However, i could only perform a boolean checking and not ignore the duplicate element.Can anyone help? May i know what should i do to solve my problem? Below is the attachment of my code and text file. Thank in advance=D

For below text file i must continue read in id: 123, 126, 65, 78, 655 and ignore the 655 duplicate record. In other words, i should read in five records instead of six.

textfile.txt
Id = 123
Name = John
Address = 465 Ripley Boulevard, Singapore 7666322
DOB = 10-10-1965
Phone Number = 123-4678
Account Balance = 405600.00

Id = 126
Name = Ben
Address = 200 Hunting Street, Singapore 784563
DOB = 25-10-1968
Phone Number = 432-4579
Account Balance = 530045.00

Id = 65
Name = Tracy
Address = 45 Mexican Boulevard, Hotel California, Singapore 467822
DOB = 06-04-73
Phone Number = 790-0000
Account Balance = 2345.00

Id = 78
Name = Mindy
Address = 50 PCK Avenue, Singapore 639798
DOB = 11-08-64
Phone Number = 345-6780
Account Balance = 0.00

Id = 655
Name = Morgan
Address = 100 Blue Eyed St, Singapore 456872
DOB = 15-02-68
Phone Number = 456-1234
Account Balance = 600.00

Id = 655
Name = Morgan
Address = 100 Blue Eyed St, Singapore 456872
DOB = 15-02-68
Phone Number = 456-1234
Account Balance = 600.00

//check existence of duplicate Account ID
                for (int i = 0; i < recSize; i++) {
                    for (int j = i + 1; j < recSize; j++) {
                        if (cust[i].getAccountID() == cust[j].getAccountID()) {

                            duplicate = true;

                        }
                        
                    }
                    
                }

                if (duplicate == false) {

                    try {
                        br = new BufferedReader(new FileReader(fileName));
                        line1 = null;

                        while ((line1 = br.readLine()) != null) {

                            StringTokenizer st2=new StringTokenizer(line1,"=");

                            while (st2.hasMoreTokens()) {
                                data = st2.nextToken();

                                if (data.equals("Account Id ")) {
                                    count++;
                                }

                            }

                        }

                        recSize2 = count;
                        System.out.println("Number of record " +
                                           "read: " + recSize2);
                    } catch (FileNotFoundException ex) {
                        
                    }

                } else if (duplicate == true) {

                    System.out.println("\nExistence of duplicate Account ID !");
                }

                br.close();

You could create a class for the information that you read in (such as a Person class) and assign the attributes to data (i.e., a String for the line being read).

Then make each Person object implement Comparable<Person>

After generating the objects based on the data retrieved from the file, store the person object in an ArrayList after doing a check to see if that object already exists--

you will have to overwrite the Comparable<Person> compareTo method and then determine when 2 people are "equal" before you can do this.

Afterwards iterate through the list and do comparisons per person. If any 2 objects compareTo be the same, then don't insert them in the list.

Also, I could be wrong, but it seems like you never set duplicate back to false. You will need to do that, otherwise once it is set to true, everything in your file will be treated like it is a duplicate.

This article has been dead for over six months. Start a new discussion instead.