469,330 Members | 1,287 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

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-
Nov 1 '07 #1
26 17082
r035198x
13,262 8TB
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.
Nov 1 '07 #2
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.
Nov 1 '07 #3
r035198x
13,262 8TB
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?
Nov 1 '07 #4
They are both text files.
Nov 1 '07 #5
They are both text files.
I put them both in an ArrayList though.
Nov 1 '07 #6
r035198x
13,262 8TB
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.
Nov 1 '07 #7
I just want to know the word changes of the two documents. Could I use the indexOf in a loop to notice the changes?
Nov 1 '07 #8
What about the contains method. What do you think?
Nov 1 '07 #9
r035198x
13,262 8TB
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

Expand|Select|Wrap|Line Numbers
  1. this
  2. was the 
  3. file before
Then later it becomes
Expand|Select|Wrap|Line Numbers
  1. was this
  2. the
  3. file before
What should the program do?
Nov 1 '07 #10
I guess I just want to see the word differences for now. Do you know an looping algorithm for this??
Nov 1 '07 #11
r035198x
13,262 8TB
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.
Nov 1 '07 #12
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-
Nov 1 '07 #13
JosAH
11,448 Expert 8TB
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
Nov 1 '07 #14
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!
Nov 1 '07 #15
r035198x
13,262 8TB
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!
You do realize that an inner loop is simply a loop started within another loop? There is no algorithm for it as such but the moment you start a loop inside another loop then that loop becomes an inner loop and the first one is the outer loop.
Nov 1 '07 #16
Yes, I have used it before, but I am not too sure about how it works to well. I was hoping someone could point me to a good article on it. I am not too familar with how it works, and I would like to better understand it.
Nov 1 '07 #17
r035198x
13,262 8TB
Yes, I have used it before, but I am not too sure about how it works to well. I was hoping someone could point me to a good article on it. I am not too familar with how it works, and I would like to better understand it.
Just go through Sun's tutorial.
Nov 1 '07 #18
I am pretty good with using one for statement in a program, but I get confused when I start using more than one. Do you have any good examples to point out for more than one?? Man, I have a lot to learn here!
Nov 1 '07 #19
r035198x
13,262 8TB
I am pretty good with using one for statement in a program, but I get confused when I start using more than one. Do you have any good examples to point out for more than one?? Man, I have a lot to learn here!
For that page that I gave you just hit next and you'll get to some parts where nested loops are used. You'll also learn some things there as well along the way. Also try out some programs that involve arrays and require nested loops. Things are generally easier to learn if you practice them on the compiler as well.
Nov 1 '07 #20
I see them now! I will take a look, and I will heed your advice on practice. I don't think I practice everything as much as I should!! Thanks again for all your help! You all are the Best!
Nov 1 '07 #21
JosAH
11,448 Expert 8TB
I don't want to discourage you but I'm afraid that a decent 'diff' algorithm is a bit
way over your head right now. First study what loops (within loops) are all about
and realize that they're nothing special. A largest common substring algorithm
(and an efficient implementation thereof) is out of reach for now.

kind regards,

Joa
Nov 1 '07 #22
I gotcha....your great help!!! thank you two -
Nov 1 '07 #23
JosAH
11,448 Expert 8TB
I gotcha....your great help!!! thank you two -
About those nested loops: think how you would visit all squares on a chessboard
or similar. Think of rectangular (non-square) boards; how would you visit all the
cells? Think of a loop per row and an inner loop per column. Try it the other way
around and see in what order the cells are visited. Try to do it with a single loop.
Print out all the index values and see what happens.

kind regards,

Jos
Nov 2 '07 #24
How to compare two files in java & uncommon thing should print in text file..

thanks
Samir
Feb 18 '08 #25
r035198x
13,262 8TB
How to compare two files in java & uncommon thing should print in text file..

thanks
Samir
Have you read this thread at all?
Feb 18 '08 #26
JosAH
11,448 Expert 8TB
How to compare two files in java & uncommon thing should print in text file..

thanks
Samir
Why ask again in someone else's thread? You also started your own thread for
your question. Stick to it please.

kind regards,

Jos
Feb 18 '08 #27

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Robin Siebler | last post: by
19 posts views Thread by David zhu | last post: by
5 posts views Thread by | last post: by
2 posts views Thread by Nitro | last post: by
5 posts views Thread by drabee | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.