Solved

Compare lines

Posted on 2004-10-04
4
159 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

930 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now