How to Compare Contents of Two Files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CodeTilYaDrop
    New Member
    • Aug 2007
    • 66

    How to Compare Contents of Two Files

    Does anyone know how to compare contents of two ArrayLists? I want to compare them to see if they match up. Do you have an example or could you point me in the right direction. I know I need to loop through it with an if statement a for loop somehow or both, but I am not sure how. thanks for any help you can provide-
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by CodeTilYaDrop
    Does anyone know how to compare contents of two ArrayLists? I want to compare them to see if they match up. Do you have an example or could you point me in the right direction. I know I need to loop through it with an if statement a for loop somehow or both, but I am not sure how. thanks for any help you can provide-
    The objects in the ArrayList are Comparable, right?
    When you say you want to see if they "match up" you mean if they contain equal objects at the same locations, right?

    Then a simple loop going through the ArrayLists should do it.

    Comment

    • CodeTilYaDrop
      New Member
      • Aug 2007
      • 66

      #3
      It is not that simple. I am trying to compare two separate documents, and find where the changes were made. I want to go through some type of loop to find the changes made on one comparred to the other. The contents may be moved all around.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by CodeTilYaDrop
        It is not that simple. I am trying to compare two separate documents, and find where the changes were made. I want to go through some type of loop to find the changes made on one comparred to the other. The contents may be moved all around.
        That is not what you said in your original post is it? So these documents are what type? .txt, .odt, e.t.c?

        Comment

        • CodeTilYaDrop
          New Member
          • Aug 2007
          • 66

          #5
          They are both text files.

          Comment

          • CodeTilYaDrop
            New Member
            • Aug 2007
            • 66

            #6
            Originally posted by CodeTilYaDrop
            They are both text files.
            I put them both in an ArrayList though.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by CodeTilYaDrop
              I put them both in an ArrayList though.
              What kind of changes are you trying to look for.
              It may be better to read the files into two StringBuilders. Open the specs for the StringBuilder class and see what can be done on it.

              Comment

              • CodeTilYaDrop
                New Member
                • Aug 2007
                • 66

                #8
                I just want to know the word changes of the two documents. Could I use the indexOf in a loop to notice the changes?

                Comment

                • CodeTilYaDrop
                  New Member
                  • Aug 2007
                  • 66

                  #9
                  What about the contains method. What do you think?

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    If you use contains, you would be testing to see if it contains what?
                    To test for word differences, just look word by word in the files and see if the words are the same.

                    OK Suppose you have the file initially as

                    Code:
                    this
                    was the 
                    file before
                    Then later it becomes
                    Code:
                    was this
                    the
                    file before
                    What should the program do?

                    Comment

                    • CodeTilYaDrop
                      New Member
                      • Aug 2007
                      • 66

                      #11
                      I guess I just want to see the word differences for now. Do you know an looping algorithm for this??

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by CodeTilYaDrop
                        I guess I just want to see the word differences for now. Do you know an looping algorithm for this??
                        You haven't answered my question yet.
                        The answer determines what (if any) kind of a loop you need.

                        Comment

                        • CodeTilYaDrop
                          New Member
                          • Aug 2007
                          • 66

                          #13
                          I am not sure exactly what I need when you put it this way. I will take a look at what I am doing, and see if I can clarify my questions. thanks for trying to help me-

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Have a look at the diff algorithm.
                            It implements the LCS (Longest Common Substring) algorithm in order to find
                            the minimal adjustments (inserts, updates, deletes) needed to transform one
                            text to the other text.

                            kind regards,

                            Jos

                            Comment

                            • CodeTilYaDrop
                              New Member
                              • Aug 2007
                              • 66

                              #15
                              You all are great helpers! I really appreciate it!

                              Can I ask you about one more thing. I was reading about an inner and outer loop to compare items in arrays. Do you know where to find the algorithm for this, too? This might help me too!

                              Comment

                              Working...