Solved

Compare lines

Posted on 2004-10-04
4
168 Views
Last Modified: 2010-03-31
How can i compare a line of a file with every line of another file?
0
Comment
Question by:mmartha
  • 2
4 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 12222676
BufferedReader in1 = new BufferedReader(new FileReader(file1));
BufferedReader in2 = new BufferedReader(new FileReader(file2));
String line1 = null;
String line2 = null;
while ((line1=in1.readLine())!=null && (line2=in2.readLine())!=null)
{
   if (line1.equals(line2))
   {
      System.out.println("match");
   }
   else
   {
      System.out.println("mismatch");
   }
}
in1.close();
in2.close();
0
 
LVL 37

Accepted Solution

by:
zzynx earned 400 total points
ID: 12224380
That compares each line of file1 with the corresponding line of file2.
The question was: "How can i compare a line of a file with *every line* of another file?

What line of the first file do you want to compare with every line of a second file?

final int NR_OF_LINE = 5;  // assuming you want to compare the 5th line of file1 with every line in file2:

BufferedReader in1 = new BufferedReader(new FileReader(file1));
BufferedReader in2 = new BufferedReader(new FileReader(file2));
String line1 = null;
String line2 = null;
int lineNr = 0;
// Get the 5th line of file1:
while ( lineNr!=NR_OF_LINE && (line1==in1.readLine())!=null )
  lineNr++;
in1.close();

// compare that line which every line of file2
while ( (line2=in2.readLine())!=null ) {
   if (line1.equals(line2)) {
      System.out.println("match");
   }
   else {
      System.out.println("mismatch");
   }
}
in2.close();
0
 
LVL 2

Author Comment

by:mmartha
ID: 12278581
zzynx,
your answer have some errors, but give me the idea.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12284307
Thanks
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question